绑定到 WPF DataGrid 中的选定项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14831616/
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
Bind to selected items in WPF DataGrid
提问by Hodaya Shalom
I want to know who are the selected items in the DataGrid (insert them to the collection in the ViewModel).
我想知道谁是 DataGrid 中的选定项(将它们插入到 ViewModel 中的集合中)。
When I bind to the selected item is changed only when I click on the row (1 row), but when I press ctrl + clicking it remains the first item, why is this happening and is it possible to link all selected items?
当我绑定到所选项目时,仅当我单击行(1 行)时才更改,但是当我按 ctrl + 单击时它仍然是第一个项目,为什么会发生这种情况,是否可以链接所有选定项目?
Here my DataGrid:
这是我的 DataGrid:
<DataGrid SelectedIndex="{Binding SelectedXIndex}" DataContext="{Binding XViewModel}" SelectedItem="{Binding CurrentX}" ItemsSource="{Binding ListX, Mode=TwoWay}" AutoGenerateColumns="False" >
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
In the XViewModel I have:
在 XViewModel 我有:
SelectedXIndex(int) for the selected index
SelectedXIndex(int) 对于所选索引
CurrentX(object of class x) for the current selection
CurrentX(类 x 的对象)用于当前选择
ListX- list of class x
ListX- 类 x 的列表
采纳答案by iltzortz
The first suggestions i made did not work. I leave them just for historical reasons.
我提出的第一个建议没有奏效。我离开他们只是出于历史原因。
I finally think that the problem is that what you want to do is kind of strange - or error prone. You put your bind to SelectedItem (one item) while you want to be able to select multiple.
我最终认为问题在于您想要做的事情有点奇怪 - 或者容易出错。当您希望能够选择多个时,您将绑定放在 SelectedItem(一个项目)上。
I would personally put no binding at all in xaml (except the ItemsSource of course) and track selection changes with the selectionChanged event in the back end code. A link with a similar problem supporting this method is found here
我个人不会在 xaml 中放置任何绑定(当然,ItemsSource 除外)并使用后端代码中的 selectionChanged 事件跟踪选择更改。在此处找到了支持此方法的类似问题的链接
Hope you find a better solution
希望你找到更好的解决方案
OLD SUGGESTIONS:
旧建议:
this should solve the issue as you seem to be treating it like a single mode selection Datagrid but you do nto explicitely define it as such:
这应该可以解决问题,因为您似乎将其视为单模式选择 Datagrid,但您没有明确地将其定义为:
<DataGrid SelectionMode="Single"
If you want the selection mode to be multiple then you should NOT bind to SelectedIndex
如果您希望选择模式为多个,则不应绑定到 SelectedIndex
I also wonder why you have both SelectedIndex binding and SelectedItem binding. In any case just one should be needed.
我也想知道为什么您同时拥有 SelectedIndex 绑定和 SelectedItem 绑定。在任何情况下,只需要一个。
Keep just the SelectedItem binding set SelectionMode to Extended and try again
保持 SelectedItem 绑定将 SelectionMode 设置为 Extended 并重试
回答by TrueEddie
Have you looked at the "SelectedItems"(not SelectedItem) collection?
你看过“SelectedItems”(不是SelectedItem)集合吗?
回答by Brian Hinchey
I have posted a technique for allowing a read-only binding to the SelectedItems property of a WPF DataGrid just by extending the DataGrid. You can see my answer at https://stackoverflow.com/a/16953833/62278
我已经发布了一种技术,该技术允许仅通过扩展 DataGrid 对 WPF DataGrid 的 SelectedItems 属性进行只读绑定。你可以在https://stackoverflow.com/a/16953833/62278看到我的回答

