在Datagrid中查找组合框 [英] Find combobox inside the Datagrid

查看:108
本文介绍了在Datagrid中查找组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI
我在wpf usercontrol中有一个datagrid.我在datagrid内有combobox datatemplate.现在,我需要将记录绑定到组合框.我在loadingrow事件中使用了这段代码

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

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;
           }

此代码在Silverlight中有效,但在wpf中,cellcontent每次都具有空值.请提供正确的代码以仅在wpf中找到数据网格内的组合框.我有这样的模板列

< datagridtemplatecolumn标头=打印机">
< datagridtemplatecolumn.celltemplate>
< datatemplate>

< combobox tag =组合" name ="cmbprinter">


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">


推荐答案

使用此功能可以列出属于数据网格的所有ComboBox类型的子代:

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

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;
            }
        }
    }
}



获得所有组合框以区分所需的组合框后,请检查其DataContext属性.它应该引用属于您所知道的行的数据项. (使用断点快速监视功能来找出DataContext 所引用的对象).

希望对您有所帮助.



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.


这篇关于在Datagrid中查找组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆