抢占DataGridTemplateColumn中元素的引用 [英] Grabbing reference to element inside DataGridTemplateColumn

查看:94
本文介绍了抢占DataGridTemplateColumn中元素的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上苦苦挣扎了一段时间,而且似乎无法获得任何答案。我已经模拟了一个简单的XAML文件来演示该问题(我没有提供真正的XAML文件,因为它包含的内容超出了此问题的需要)。

I've been struggling with this problem for a while now and can't seem to get any answers on it. I've mocked up a simple XAML file to demonstrate the problem (I'm not providing my real XAML file since it has more in it than needed for this question).

这是我的问题:给定以下XAML文件,如何在我的代码背后获取对位于DataGridTemplateColumn.CellEditingTemplate DataTemplate中的selectHeight ComboBox的引用?我需要引用,以便可以根据selectAge ComboBox中的选择更改ComboBox的属性。

Here is my question: given the following XAML file how can I get a reference in my codebehind to the selectHeight ComboBox that resides in the DataGridTemplateColumn.CellEditingTemplate DataTemplate? I need the reference so that I can change a property of the ComboBox based on a selection in the selectAge ComboBox.

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid Name="grdPeople"
                    AutoGenerateColumns="False"  
                    AlternatingRowBackground="LightBlue" 
                    CanUserAddRows="True" CanUserDeleteRows="True" IsEnabled="True"
                    MaxHeight="400" VerticalScrollBarVisibility="Auto">

        <DataGrid.Columns>
            <!--Name Column-->
            <DataGridTemplateColumn Header="MyName" CanUserReorder="False" CanUserResize="False"  CanUserSort="False" >
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <Label Content="{Binding Path=Name}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate >
                    <DataTemplate>
                        <TextBox Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <!--Age Column-->
            <DataGridTemplateColumn Header="MyAge">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=Age}"></Label>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox Name="selectAge" ItemsSource="{Binding Path=AgeOpts}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding Path=Age}"></Label>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>    
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <!--Height Column-->
            <DataGridTemplateColumn Header="MyHeight">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Path=Height}"></Label>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox Name="selectHeight" ItemsSource="{Binding Path=HeightOpts}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding Path=Height}"></Label>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

我意识到我在做什么我尝试做的可能并非完全是最佳实践,但我正在尝试使用遗留代码中给出的内容。

I realize that what I'm trying to do may not be exactly "best practice" but I'm trying to work with what I've been given in legacy code.

我发现此MSDN页面与我想做的事情有关,但我不知道如何将示例转换为我的方案: http://msdn.microsoft.com/zh-cn/library/system.windows。 frameworktemplate.findname.aspx

I found this MSDN page relating to what I want to do but I can't figure out how to translate the example into my scenario: http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx.

任何帮助将不胜感激!谢谢!

Any help would be appreciated! Thanks!

推荐答案

这是我喜欢用来浏览WPF可视树并查找控件的库脚本。

Here's a library script I like using to navigate WPF's visual tree and find controls.

您可以这样使用: ComboBox cbx = FindChild< ComboBox>(grdPeople, selectHeight);

如果找不到任何 ComboBox null c>名为 selectHeight ,因此在使用它之前,请务必检查 cbx == null 。 p>

It will return null if it can't find any ComboBox that is named "selectHeight" so be sure to check if cbx == null before using it.

public static T FindChild<T>(DependencyObject parent, string childName)
    where T : DependencyObject
{
    // Confirm parent and childName are valid. 
    if (parent == null) return null;

    T foundChild = null;

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        // If the child is not of the request child type child
        T childType = child as T;
        if (childType == null)
        {
            // recursively drill down the tree
            foundChild = FindChild<T>(child, childName);

            // If the child is found, break so we do not overwrite the found child. 
            if (foundChild != null) break;
        }
        else if (!string.IsNullOrEmpty(childName))
        {
            var frameworkElement = child as FrameworkElement;
            // If the child's name is set for search
            if (frameworkElement != null && frameworkElement.Name == childName)
            {
                // if the child's name is of the request name
                foundChild = (T)child;
                break;
            }
            else
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);

                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null) break;
            }
        }
        else
        {
            // child element found.
            foundChild = (T)child;
            break;
        }
    }

    return foundChild;
}

public static T FindChild<T>(DependencyObject parent)
    where T : DependencyObject
{
    // Confirm parent is valid. 
    if (parent == null) return null;

    T foundChild = null;

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        // If the child is not of the request child type child
        T childType = child as T;
        if (childType == null)
        {
            // recursively drill down the tree
            foundChild = FindChild<T>(child);

            // If the child is found, break so we do not overwrite the found child. 
            if (foundChild != null) break;
        }
        else
        {
            // child element found.
            foundChild = (T)child;
            break;
        }
    }
    return foundChild;
}

这篇关于抢占DataGridTemplateColumn中元素的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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