WPF 数据网格 selectedvalue/selecteditem 绑定不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24161174/
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
WPF datagrid selectedvalue/selecteditem bindings does not work
提问by makambi
I have my datagrid:
我有我的数据网格:
<DataGrid CanUserSortColumns="False" x:Name="TablePreviewDataGrid"
SelectionMode="Single"
SelectionUnit="CellOrRowHeader"
SelectedValuePath="{Binding Item}"
SelectedCellsChanged="DataGrid_OnSelectedCellsChanged"
IsReadOnly="True" ItemsSource="{Binding TableData}"
AutoGenerateColumns="True">
<DataGrid.Resources>
<Style TargetType="DataGridColumnHeader">
<EventSetter Event="Click" Handler="EventSetter_OnHandler" />
</Style>
</DataGrid.Resources>
</DataGrid>
And bindings:
和绑定:
private DataTable _tableData;
public DataTable TableData
{
get { return _tableData; }
set { SetProperty(ref _tableData, value); }
}
private object _selectedItem;
public object Item
{
get { return _selectedItem; }
set { SetProperty(ref _selectedItem, value); }
}
Itemproperty is not updated when I click on grid cells. Howewer, if I remove
Item单击网格单元格时,属性不会更新。但是,如果我删除
SelectionMode="Single"
SelectionUnit="CellOrRowHeader"
binding works just fine.
绑定工作得很好。
Does someone knows why is this happening?
有人知道为什么会这样吗?
回答by Sheridan
MSDN knows. If you look at the DataGrid.SelectionUnitPropertypage on MSDN, it says:
MSDN 知道。如果您查看MSDN上的“DataGrid.SelectionUnit属性”页面,它会说:
Gets or sets a value that indicates whether rows, cells, or both can be selected in the
DataGrid.
获取或设置一个值,该值指示是否可以在
DataGrid.
The most usual (and default) value is FullRow, so you could try this.
最常用的(也是默认的)值是FullRow,所以你可以试试这个。
If you look at the Selector.SelectedValuePropertypage on MSDN, it sayd:
如果您查看MSDN上的“Selector.SelectedValue属性”页面,它会说:
Gets or sets the value of the
SelectedItem, obtained by usingSelectedValuePath.
获取或设置
SelectedItem使用 获得的的值SelectedValuePath。
So your first problem is that you didn't set the SelectedValuePathproperty to the name of the property that you want passed into the SelectedValueproperty. However, it is more usual to data bind an object of the same type as the items in your DataGrid.ItemsSoucrecollection to the SelectedItemproperty, then you can bind to the whole selected item object.
所以你的第一个问题是你没有将SelectedValuePath属性设置为你想要传递给SelectedValue属性的属性的名称。但是,更常见的是将与DataGrid.ItemsSoucre集合中项目相同类型的对象数据绑定到SelectedItem属性,然后您可以绑定到整个选定的项目对象。
I doubt that removing the SelectionModeproperty would make any difference in this case. So to fix your problem, I'd set the DataGrid.SelectionUnitProperty to FullRowand data bind to the SelectedItemproperty instead of using the SelectedValueand SelectedValuePathproperties. You can find out further differences between these selected item properties in the How to: Use SelectedValue, SelectedValuePath, and SelectedItempage on MSDN.
我怀疑SelectionMode在这种情况下删除该属性会产生任何影响。因此,为了解决您的问题,我将DataGrid.SelectionUnit属性设置为FullRow和数据绑定到SelectedItem属性,而不是使用SelectedValue和SelectedValuePath属性。您可以在 MSDN 上的如何:使用 SelectedValue、SelectedValuePath 和 SelectedItem页面中找到这些选定项属性之间的进一步差异。
回答by Nitin
Your culprits are SelectionUnit="CellOrRowHeader"and SelectedValuePath="{Binding Item}".
您的罪魁祸首是SelectionUnit="CellOrRowHeader"和SelectedValuePath="{Binding Item}".
Since each row in DataGridrepresents the data item, hence selecting full row will select the item or SelectionUnit="FullRow"will select the item. Else if you have other SelectionUnit then you can get SelectedItemby DataGrid.SelectedCells[0].Item
由于每行都DataGrid代表数据项,因此选择整行将选择该项目或SelectionUnit="FullRow"将选择该项目。否则,如果您有其他选择单元,那么您可以SelectedItem通过DataGrid.SelectedCells[0].Item
Also you don't bind SelectedValuePathbut SelectedValue
你也没有绑定SelectedValuePath但是SelectedValue

