我一直在努力解决这个问题一段时间,似乎无法得到任何答案。我已经模拟了一个简单的 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。查找名称.aspx。
任何帮助,将不胜感激!谢谢!