获取对 DataGridTemplateColumn 内元素的引用
我已经为这个问题苦苦挣扎了一段时间,似乎无法得到任何答案。我模拟了一个简单的 XAML 文件来演示该问题(我没有提供真正的 XAML 文件,因为它包含的内容超出了该问题所需的内容)。
这是我的问题:给定以下 XAML 文件,如何在代码隐藏中获取对驻留在 DataGridTemplateColumn.CellEditingTemplate DataTemplate 中的 selectHeight ComboBox 的引用?我需要参考,以便我可以根据 selectAge ComboBox 中的选择更改 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>
我意识到我想做的可能并不完全是“最佳实践”,但我正在尝试使用遗留代码中给出的内容。
我发现这个 MSDN 页面与我想要做的事情相关,但我不知道如何将示例转换为我的场景: http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx。
任何帮助将不胜感激!谢谢!
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).
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个我喜欢用来导航 WPF 可视化树并查找控件的库脚本。
您可以这样使用它:
ComboBox cbx = FindChild(grdPeople, "selectHeight");
如果找不到任何
ComboBox,它将返回
名为null
"selectHeight"
,因此在使用它之前一定要检查cbx == null
。Here's a library script I like using to navigate WPF's visual tree and find controls.
You use it like this:
ComboBox cbx = FindChild<ComboBox>(grdPeople, "selectHeight");
It will return
null
if it can't find anyComboBox
that is named"selectHeight"
so be sure to check ifcbx == null
before using it.