Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从wpf中的子gridspanels访问子值_C#_Wpf_Grid_Telerik - Fatal编程技术网

C# 从wpf中的子gridspanels访问子值

C# 从wpf中的子gridspanels访问子值,c#,wpf,grid,telerik,C#,Wpf,Grid,Telerik,请帮助我如何访问C中的子项# 这里我想用C语言访问Cmb#u Name,txtAddress,txtAdditionAddress,但它们在C语言中无法访问,因为它们在内部网格中,所以请告诉我如何访问内部网格您的数据模板将被膨胀,然后添加到“datform”的可视树中。因此,您可以通过使用visualtreeheloper类递归搜索可视化树来查找这些元素 概念验证代码: private void ButtonBase_OnClick(object sender, RoutedEven

请帮助我如何访问C中的子项#



这里我想用C语言访问
Cmb#u Name
txtAddress
txtAdditionAddress
,但它们在C语言中无法访问,因为它们在内部网格中,所以请告诉我如何访问内部网格

您的
数据模板
将被膨胀,然后添加到“datform”的可视树中。因此,您可以通过使用
visualtreeheloper
类递归搜索可视化树来查找这些元素

概念验证代码:

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        // Find all TextBox  visual children named "txtAddress" starting with the datform control
        var txtAddressMatches = GetVisualChildWithName("txtAddress", datform).OfType<TextBox>();

        // Fina all ComboBox visual children named "Cm_Name" starting with the datform control
        var Cmb_NameMatches = GetVisualChildWithName("Cm_Name", datform).OfType<ComboBox>();
    }

    private IEnumerable<FrameworkElement> GetVisualChildWithName(string name, FrameworkElement element)
    {
        return GetVisualChildWithName(name, element, new List<FrameworkElement>());
    }

    private IEnumerable<FrameworkElement> GetVisualChildWithName(string name, FrameworkElement element, IEnumerable<FrameworkElement> matches) 
    {
        if (element == null)
        {
            return matches;
        }

        if (element.Name == name)
        {
            matches = matches.Concat(new []{element});
        }

        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
        {
            matches = matches.Concat(GetVisualChildWithName(name, VisualTreeHelper.GetChild(element, i) as FrameworkElement, new List<FrameworkElement>()));
        }

        return matches;
    }
private void按钮base\u OnClick(对象发送方,路由目标)
{
//查找以datform控件开头的名为“txtAddress”的所有文本框可视子项
var txtAddressMatches=GetVisualChildWithName(“txtAddress”,datform).OfType();
//Fina所有组合框视觉子项命名为“Cm_Name”,以datform控件开头
var Cmb_Name matches=GetVisualChildWithName(“Cm_Name”,datform).OfType();
}
私有IEnumerable GetVisualChildWithName(字符串名称,FrameworkElement元素)
{
返回GetVisualChildWithName(名称、元素、新列表());
}
私有IEnumerable GetVisualChildWithName(字符串名称、FrameworkElement元素、IEnumerable匹配项)
{
if(元素==null)
{
返回比赛;
}
if(element.Name==Name)
{
matches=matches.Concat(新[]{element});
}
for(变量i=0;i
VisualTreeHelper
类具有静态方法,允许您发现给定项(以及其他项)的所有子项和父项关系。更多信息可在此处找到:

请记住,在遍历可视化树时,无法保证找到唯一的名称,因此在上面的示例代码中,我将返回一个匹配列表。例如,如果将
DataTemplate
中的命名元素应用于
ItemsControl
中的每个项,就会发生这种情况。如果我们开始在
ItemsControl
中搜索可视化树,我们将在模板中为每个容器项目发现一次命名项目(例如
ListBoxItem

修改上面的代码以获得搜索深度将是微不足道的,但希望您能理解:)

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        // Find all TextBox  visual children named "txtAddress" starting with the datform control
        var txtAddressMatches = GetVisualChildWithName("txtAddress", datform).OfType<TextBox>();

        // Fina all ComboBox visual children named "Cm_Name" starting with the datform control
        var Cmb_NameMatches = GetVisualChildWithName("Cm_Name", datform).OfType<ComboBox>();
    }

    private IEnumerable<FrameworkElement> GetVisualChildWithName(string name, FrameworkElement element)
    {
        return GetVisualChildWithName(name, element, new List<FrameworkElement>());
    }

    private IEnumerable<FrameworkElement> GetVisualChildWithName(string name, FrameworkElement element, IEnumerable<FrameworkElement> matches) 
    {
        if (element == null)
        {
            return matches;
        }

        if (element.Name == name)
        {
            matches = matches.Concat(new []{element});
        }

        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
        {
            matches = matches.Concat(GetVisualChildWithName(name, VisualTreeHelper.GetChild(element, i) as FrameworkElement, new List<FrameworkElement>()));
        }

        return matches;
    }