如何获取 WPF DataGrid 的单元格级别 ComboBox?
看来 WFP DataGridComboBoxColumn 对此列中的所有单元格使用单个 ItemsSource。我有一个情况,组合框项目依赖于同一行中的其他单元格。我设法在 PreparingCellForEdit 事件中填充 ItemsSource。但是,它并没有按预期工作。最初,该列中的所有单元格都是空的。一旦我填充此列的 ComboBox 的 ItemsSource,所有相关单元格(具有相同的项目源)都会显示值。但是,如果我单击另一种类型的单元格(填充不同的项目源),所有值都会消失,新类型的单元格会显示值。一列只能使用一组项目源?我不敢相信这是真的。我错过了什么吗?有什么解决办法吗?
It looks WFP DataGridComboBoxColumn is using a single ItemsSource for all cells in this column. I have a case where the ComboBox items are dependent on the other cell in the same row. I managed to populate the ItemsSource in PreparingCellForEdit event. However, it doesn't work as desired. Initially, all cells in this column is empty. Once I populate the ItemsSource for this column's ComboBox, all related cells (with the same items source) are showing values. However, if I click another type of cell (a different items source is populated), all values disappear and the new type cells show values. You can only use one set of Items Source for a column? I can't believe it is true. Did I miss anything? Any workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可能无法可靠地做到这一点。网格可以重用组合框或随机创建/销毁它。
一次偶然的机会,我正好在一个可以实现这一功能的屏幕上工作。鉴于这些...
这使我能够将 ItemsSource 绑定到 TerritoryCanidates 属性。 DataGrid 在所有情况下都会遵循这一点。
You probably can't do it reliably. The grid may reuse the combo box or randomly create/destroy it.
By chance I just happen to be working on a screen that does just that. Given these...
This gives me the ability to bind the ItemsSource to the TerritoryCanidates property. Which in turn the DataGrid will honor under all circumstances.
感谢乔纳森的例子,我解决了我的问题如下。我修改了 Jonathan 的代码以突出显示我的解决方案。我从他的示例中删除了 Territory 属性,因为我的问题不需要它。
有两列。第一列是状态。第二列是 StateCandidate。 State 列绑定到 States 列表,StateCandidate 列绑定到 StateCandidates 列表。关键点是,当 State 更改时,会重新创建 StateCandidates 列表。因此,每行中可能有不同的 StateCandidates 列表(基于所选的 State)。
MainWindow.xaml
MainWindow.xaml.cs
请注意,当状态更改时,它不会更新 StateCandidates 列表,直到选择不同的行,这是我将要解决的一个单独问题。有人知道我如何强制提交吗?
再次感谢乔纳森的灵感。我会继续寻找更好的解决方案。
Thanks to Jonathan's example, I resolved my problem as follows. I modifiedy Jonathan's code to highlight my solution. I removed the Territory property from his example because I don't need it for my problem.
There are two columns. First column is State. Second column is StateCandidate. State column is bind to a States list, and StateCandidate column is bind to a StateCandidates list. The key point is that the StateCandidates list is recreated when State is changed. So, there could be a different list of StateCandidates in each row (based on the selected State).
MainWindow.xaml
MainWindow.xaml.cs
Note that, when State is changed, it won't update StateCandidates list until a different row is selected, which is a separated issue I'll be fighting. Do anybody know how I can force commit?
Thanks to Jonathan again for his inspiration. I'll keep looking for a better solution.