wpf DataGrid SelectedItem 未更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28298781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGrid SelectedItem not updating
提问by Anthony Russell
So I am reallly confused here.
所以我真的很困惑。
I created a datagrid, bound its itemsource two way and bound its selected item two way. The selected item getter gets called but the setter never does. All the pieces seem to be here. What am I missing?
我创建了一个数据网格,以两种方式绑定其 itemsource 并以两种方式绑定其选定的项目。选定的项 getter 被调用,但 setter 永远不会调用。所有的碎片似乎都在这里。我错过了什么?
<DataGrid ItemsSource="{Binding Properties ,Mode=TwoWay}"
SelectedItem="{Binding SelectedProperty ,Mode=TwoWay}"
CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False" Background="LightBlue">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Header="Address" Binding="{Binding Address}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Units ,Mode=TwoWay}"
SelectedItem="{Binding SelectedUnit, Mode=TwoWay}"
CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False">
My first datagrid works fine including the selected item.
我的第一个数据网格工作正常,包括所选项目。
The second third and fourth nested grids however don't bind to the selected item. The items sources work but that is it
但是,第二个第三个和第四个嵌套网格不绑定到所选项目。项目来源有效,但就是这样
public class PropertyModel : ModelBase
{
private ObservableCollection<UnitModel> _Units;
public ObservableCollection<UnitModel> Units
{
get { return _Units; }
set { _Units = value; }
}
private UnitModel _SelectedUnit;
public UnitModel SelectedUnit
{
get { return _SelectedUnit; }
set { _SelectedUnit = value; OnPropertyChanged("SelectedUnit"); }
}
There are no binding expression errors or any other errors displayed in the output window.
输出窗口中没有显示绑定表达式错误或任何其他错误。
回答by Anthony Russell
The answer to this is pretty obvious actually.
这个问题的答案其实很明显。
I forgot to put UpdateSourceTrigger=PropertyChanged
我忘了把 UpdateSourceTrigger=PropertyChanged
so it should look like this
所以它应该是这样的
<DataGrid ItemsSource="{Binding Units ,Mode=TwoWay}"
SelectedItem="{Binding SelectedUnit,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False">
This solved the problem
这解决了问题
回答by ELH
I faced something similar to that a couple of months ago, for some reason the inner DataGridItemSourcewasn't set correctly using that way, i managed to fix that by binding using ElementNameto set the RowDetail DataGridItemSourceto the SelectedItemin the parent one :
几个月前我遇到了类似的事情,由于某种原因,DataGridItemSource使用这种方式没有正确设置内部,我设法通过绑定使用ElementName将 RowDetail 设置为父级DataGridItemSource中的RowDetail来解决这个SelectedItem问题:
<DataGrid x:Name="DataGrid" ItemsSource="{Binding Properties ,Mode=TwoWay}"
SelectedItem="{Binding SelectedProperty ,Mode=TwoWay}"
CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False" Background="LightBlue">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Header="Address" Binding="{Binding Address}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding SelectedItem.Units ,Mode=TwoWay,ElementName=DataGrid}"

