C# WPF-获取DataGridTemplateColumn中Combobox控件的行索引
我想知道怎么做。 我有一个C# WPF-获取DataGridTemplateColumn中Combobox控件的行索引,c#,wpf,datagrid,combobox,C#,Wpf,Datagrid,Combobox,我想知道怎么做。 我有一个DataGridTemplateColumn,里面有一个简单的组合框控件。 组合框有一个链接到它的SelectionChanged事件 在changed事件中,我想知道被修改行的行索引是从被修改的组合框中派生出来的 我是不是走错了路? 以下是我所拥有的: <DataGrid AutoGenerateColumns="False" Margin="5,10,5,5" x:Name="dgrMatches" ItemsSource="{Bind
DataGridTemplateColumn
,里面有一个简单的组合框
控件。
组合框有一个链接到它的SelectionChanged
事件
在changed事件中,我想知道被修改行的行索引是从被修改的组合框中派生出来的
我是不是走错了路?
以下是我所拥有的:
<DataGrid AutoGenerateColumns="False" Margin="5,10,5,5"
x:Name="dgrMatches" ItemsSource="{Binding .}"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False"
SelectionMode="Single" SelectionUnit="FullRow" IsReadOnly="False"
RowStyle="{DynamicResource EditableRows}" CellStyle="{DynamicResource EditableTableCells}">
<DataGrid.Columns>
<DataGridTextColumn ... />
<DataGridTemplateColumn Header="Legs won" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="cbbLegsWonA"
SelectedIndex="{Binding LegsWonA, Mode=TwoWay}"
ItemsSource="{Binding NumberOfLegs}"
SelectionChanged="cbbLegsWonA_SelectionChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- @Chris Eelmaa -->
<DataGridTemplateColumn Header="Legs won" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="cbbLegsWonB"
SelectedIndex="{Binding LegsWonB, Mode=TwoWay}"
ItemsSource="{Binding NumberOfLegs}"
SelectionChanged="cbbLegsWonB_SelectionChanged" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn ... />
</DataGrid.Columns>
</DataGrid>
这不起作用:(DataGridRow)dFormatches.ContainerRomement(cbbLegsA)=null
(DataGridRow)dFormatches.ContainerRomement(cbbLegsA)==null
不起作用,因为特定行的DataGrid的ItemContainer不是DataTemplate中的组合框,它是一个包含组合框“模板化”版本的DataGridRow
。相反,您需要使用.FindParent()从组合框中查找DataGridRow(因为您的组合框位于DataGridRow的可视树中,而不是逻辑树中)。您可以从DataGridRow引用中轻松找到行索引。然而
评论中建议的更好的方法是使用MVVM模式。组合框将绑定到ViewModel中的属性。当ViewModel中的一个属性发生更改时,您可以轻松地更新另一个属性以实现所需的行为,而无需在可视化树中进行任何难看的搜索,也无需隐藏大量UI代码。它不是一个组合框自动更新UI中另一个组合框的逻辑,而是在一个更容易控制视图对象模型(也称为“视图模型”)的位置。取决于您如何定义“旋转方法”。这当然不是一种与WPF密切相关的MVVM方法。重构一切以使用MVVM模式,并首先解释为什么需要这些信息,然后我可以提供一些提示。我省略了一些细节,这是真的。我不太清楚MVVM到底是什么意思。但是我有两个
DataGridTemplateColumn
s,其组合框与问题中的一个类似。我想在第一个组合框更改时更改第二个组合框的值,反之亦然。这就是为什么我需要行索引,这样我就可以通过直接更改数据网格中的值来更改第二个组合框。@ChrisEelmaa我更新了数据网格,为您提供了一个理想值。datagrid绑定到相应更新的对象列表。然后数据网格被刷新(更多细节请参见事件hanlder)。我一直在研究MVVM,这实际上是我在应用程序中所做的(某种程度上),但有一些关键元素我没有应用。我现在已经设置好了模型、视图、视图模型和视图结构。正如你所建议的,它的工作没有任何讨厌的树横穿!谢谢
private void cbbLegsWonA_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox cbbLegsA = e.Source as ComboBox; // Altered combobox
int rowIndex = -1;
if (cbbLegsA.Tag == null)
{
DataGridRow row = (DataGridRow)dgrMatches.ContainerFromElement(cbbLegsA);
rowIndex = row.GetIndex();
cbbLegsA.Tag = rowIndex;
}
else
{
Int32.TryParse(cbbLegsA.Tag.ToString(), out rowIndex);
}
//@ChrisEelmaa: Basically, change the bound list and refresh the items in the datagrid
//The debugger doesn't get to this point, ofcourse
SingleMatch match = matches.ElementAt(rowIndex); // Get the current match out of the bound list
match.LegsWonA = cbbLegsA.SelectedIndex; // Manually change second combobox item
dgrMatches.Items.Refresh();
...
}