Click here to Skip to main content
15,849,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI
i have one datagrid in wpf usercontrol. I have the combobox datatemplate inside the datagrid. Now i need to bind records to the combobox. I used this code in loadingrow event

C#
foreach (DataGridColumn col in DataGrid_Customer.Columns)
           {
               FrameworkElement cellContent = col.GetCellContent(row);
               ComboBox combo = cellContent as ComboBox;
               if (combo != null)
               {
                   if (combo.Tag.ToString() == "Combo")
                   {
                       combo.ItemsSource = PreLoadedUserList;
                       combo.DisplayMemberPath = "Name";
                   }
               }
               //TextBox tbitem = cellContent as TextBox;
           }
this code is worked in silverlight but in wpf cellcontent is having null value at every time. please give the correct code to find the combobox inside the datagrid in wpf only. i have the template column like this

<datagridtemplatecolumn header="Printer">
<datagridtemplatecolumn.celltemplate>
<datatemplate>

<combobox tag="Combo" name="cmbprinter">


Posted
Updated 24-Mar-12 8:35am
v2
Comments
Sergey Alexandrovich Kryukov 23-Mar-12 2:26am    
"Find..." or "Bind..." (in the title)?
--SA
janablisslogix 23-Mar-12 2:51am    
Find and also Bind. First i find the control and after i need to Bind. If possible to bind without finding combobox is also welcome
Sergey Alexandrovich Kryukov 23-Mar-12 3:03am    
Why find then? You can give any control a name, in XAML.
And if you create controls in code, you could store a reference to is anywhere you want at the moment of construction. So, the "find" part is solved...
--SA
janablisslogix 23-Mar-12 3:11am    
how can i find the combobox inside the datagrid template column not in grid. thats why i am asking find and Bind.I think We can not use the reference for the combobox inside the datagrid.
Sergey Alexandrovich Kryukov 23-Mar-12 11:56am    
Why do you think so, may I ask you? :-)
--SA

1 solution

Use this function to list all children of type ComboBox belonging to the data grid :

C#
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}


After you get all the combo boxes to distinguish the desired one from the others check their DataContext property. It should refer to the data item which belongs to the row that you know. (use break point and quick watch features to find out to which object DataContext refers).

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900