This is a migrated thread and some comments may be shown as answers.

Add event to datatemplate in code-behind

6 Answers 176 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dan Harvey
Top achievements
Rank 2
Dan Harvey asked on 08 Jun 2010, 02:18 PM
Hello,

I was reading the following article, http://www.telerik.com/community/forums/silverlight/treelist/add-event-to-datatemplate-in-code-behind.aspx, and I was wondering if this was possible to do with the RadGridView.  I want to add the Checked and Unchecked events to my checkbox columns.  Here is my code: (Where dgContacts is my dataGrid and "cb"+ct is the name of my checkbox)

private void AddColumns()        
        {        
                for (var ct = 0; ct < 10; ct++)        
                {        
                    var column = new GridViewDataColumn();        
                    column.Header = "This is a List" + ct;        
                    var myXamlString = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><CheckBox x:Name=\"cb" + ct + "\" IsThreeState=\"False\" VerticalAlignment=\"Center\" IsChecked=\"{Binding List" + ct + ", Mode=TwoWay}\"/></DataTemplate>";        
                    var template = (DataTemplate) XamlReader.Load(myXamlString);        
                    column.CellTemplate = template;        
                    column.DataMemberBinding = new System.Windows.Data.Binding("List" + ct);        
                    dgContacts.Columns.Add(column);                       
                    GetChildObject<CheckBox>(dgContacts, "cb" + ct);   
   
                }     
        }    
 


 public void GetChildObject<T>(DependencyObject obj, string name) where T : DependencyObject  
        {  
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)  
            {  
                DependencyObject c = VisualTreeHelper.GetChild(obj, i);  
                if (c.GetType().Equals(typeof(T)) && (string.IsNullOrEmpty(name) || ((FrameworkElement)c).Name == name))  
                {  
                    CheckBox chk = (CheckBox)c  
                    chk.Checked += new RoutedEventHandler(CheckBox_Checked);  
                    continue;  
                }  
                GetChildObject<T>(c, name);  
            }  
 
        } 

However, what happens is that the count of the "VisualTreeHelper.GetChildernCount(obj)" is 0 everytime.

Is there a way to add events to this checkbox???

Thanks,

6 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Jun 2010, 02:23 PM
Hello,

 It will be better to create your own custom column, inherit from desired column type, override CreateCellElement/CreateCellEditElement and return desired FrameworkElement. You can check this blog post for more info.

Best wishes,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Dan Harvey
Top achievements
Rank 2
answered on 08 Jun 2010, 03:05 PM

I am not sure how to implement this approach.  I need to use a dynamic dataTemplate because I will not know how many columns my datasource will have each time the application is loaded.  I could have from 0 to 10 checkbox columns depending on what my stored proc returns. 

private void GridMainLoaded(object sender, RoutedEventArgs e)  
        {  
            PopulateGrid();  
        }  

private ObservableCollection<Contact> _contacts = new ObservableCollection<Contact>();
private void PopulateGrid()  
        {  
            var contact1 = new Contact(1, "Dan", "Stark", "STARK", true, "Flight", false, 3, true, false, true, false, true, true, true, true, true, true);  
            var contact2 = new Contact(1, "Jim", "Stark", "STARK", true, "Flight", false, 3, true, false, true, false, true, true, true, false, true, false);  
            var contact3 = new Contact(1, "Steve", "Stark", "STARK", true, "Flight", false, 3, true, false, true, false, false, true, true, true, true, true);  
            var contact4 = new Contact(1, "Vern", "Stark", "STARK", true, "Flight", false, 3, false, false, false, false, true, true, true, true, false, true);  
 
            _contacts.Add(contact1);  
            _contacts.Add(contact2);  
            _contacts.Add(contact3);  
            _contacts.Add(contact4);  
 
            AddColumns();  
            dgContacts.ItemsSource = _contacts;  
        }  
 
private void AddColumns()  
        {  
                for (var ct = 0; ct < 10; ct++)  //Where the number 10 could be any number 0 through 10
                {  
 
                    var column = new CheckBoxColumn(); //This is my new class  
                    column.Header = "This is a List" + ct;  
                    var myXamlString = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"><CheckBox x:Name=\"cb" + ct + "\" IsThreeState=\"False\" VerticalAlignment=\"Center\" IsChecked=\"{Binding List" + ct + ", Mode=TwoWay}\"/></DataTemplate>";  
                    var template = (DataTemplate) XamlReader.Load(myXamlString);  
                    column.CellTemplate = template;  
                    column.DataMemberBinding = new System.Windows.Data.Binding("List" + ct);  
                    dgContacts.Columns.Add(column);  
                }  
        } 

My new class:
public class CheckBoxColumn : GridViewDataColumn  
    {  
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)  
        {  
            var chkbx = cell.Content as CheckBox;  
 
            if (chkbx == null)  
            {  
                chkbx = new CheckBox();  
                chkbx.IsThreeState = false;  
                cell.Content = chkbx;  
            }  
 
            return chkbx;  
        }  
 
        //public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)  
        //{  
              
        //}  
 
    } 


From here, is where i get confused.  What is the next step? 

How do I use this new class in my existing code to capture the checked and unchecked events of these checkboxes?

What other code do i need to add to my new class?

Thanks!
0
Timothy
Top achievements
Rank 1
answered on 02 Dec 2010, 09:29 PM
Did you ever figure this out. I am having the exact same problem.

Thanks,
Tim
0
Juliana
Top achievements
Rank 1
answered on 17 May 2011, 09:30 AM
So do I. Has anybody solved the problem?
Juliana
0
Dan Harvey
Top achievements
Rank 2
answered on 24 May 2011, 06:33 PM

Hello Juliana,

I did solve this problem and used the following approach in code behind:



private void AddColumns(ObservableCollection<Contact> contacts)
{
    if (contacts.Count <= 0) return;
    for (var ct = 0; ct < contacts[0].Lists.Count; ct++)
    {
        var column = new DataGridTemplateColumn();
        column.Header = contacts[0].Lists[ct].ListName;
        column.HeaderStyle = (Style)this.Resources["ListColumnHeaderStyle"];
        var myXamlString = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><CheckBox HorizontalAlignment=\"Center\"   IsEnabled=\"{Binding Enabled" + ct + "}\" VerticalAlignment=\"Center\" IsChecked=\"{Binding List" + ct + ", Mode=TwoWay}\"/></DataTemplate>";
        var template = (DataTemplate)XamlReader.Load(myXamlString);
        column.CellTemplate = template;
        column.IsReadOnly = false;
        column.SortMemberPath = "List" + ct;
        dgContacts.Columns.Add(column);
    }
}
0
Juliana
Top achievements
Rank 1
answered on 02 Jun 2011, 07:52 AM
Hi Dan,
Thanks for sharing
Tags
GridView
Asked by
Dan Harvey
Top achievements
Rank 2
Answers by
Vlad
Telerik team
Dan Harvey
Top achievements
Rank 2
Timothy
Top achievements
Rank 1
Juliana
Top achievements
Rank 1
Share this question
or