简体   繁体   English

WPF Datagrid使用组合框限制输入

[英]WPF Datagrid restrict input with combobox

i am using the EF and the MVVM approach to get a bunch of data records in an ObservableCollection . 我正在使用EF和MVVM方法在ObservableCollection获取一堆数据记录。 This ObservableCollection lives in a ViewModel. ObservableCollection位于ViewModel中。 The ViewModel is than bound to a DataGrid . 然后将ViewModel绑定到DataGrid This works just fine. 这样很好。

For the sake of data consistency i now want to turn a bunch of columns in the DataGrid into ComboBoxes . 为了数据一致性,我现在想将DataGrid的一堆列转换为ComboBoxes So the user can only choose from a predefined set of values. 因此,用户只能从预定义的一组值中进行选择。 These predefined values are also stored in the database (You can think of it as a lookup table). 这些预定义的值也存储在数据库中(您可以将其视为查找表)。 I can load the values from the database and also put it in an ObservableCollection . 我可以从数据库中加载值,也可以将其放入ObservableCollection But now i wonder how can i bind this to the DataGrid , since i can only specify one ItemSource for the DataGrid . 但是现在我不知道如何将其绑定到DataGrid ,因为我只能为DataGrid指定一个ItemSource Is this even the right approach in WPF? 这在WPF中甚至是正确的方法吗?

Bind the ItemsSource property of DataGridComboBoxColumn to your values from the lookup table. DataGridComboBoxColumnItemsSource属性绑定到查找表中的值。

Then bind SelectedValueBinding to the field in the main collection. 然后将SelectedValueBinding绑定到主集合中的字段。

If you are using normalised values (ie numeric values mapped to the items in the lookup table), then you should also set the SelectedValuePath and DisplayMemberPath properties. 如果使用归一化值(即,映射到查找表中项目的数值),则还应该设置SelectedValuePathDisplayMemberPath属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM