简体   繁体   English

带有MVVM的Datagrid中的WPF级联组合框

[英]wpf cascaded combobox in Datagrid with MVVM

I am looking for binding few cascaded combobox in a WPF GridView: 我正在寻找在WPF GridView中绑定一些级联的组合框:

Following is my requirement: 以下是我的要求:

I want to develop a grid view of persons records, which would hold their Name, state name and then City name. 我想开发人员记录的网格视图,其中将包含他们的姓名,州名和城市名。 And of-course, City combobox data would depend on the State selected. 当然,城市组合框数据将取决于所选的州。

The data grid item source would be an ObservableCollection, the Person class would look like this: 数据网格项源将是一个ObservableCollection,Person类将如下所示:

class Person
{
    name string;
    state string;
    city string;
}

And View Model for the Datagrid Window would look like this: 数据网格窗口的视图模型将如下所示:

class DatagridWindowViewModel
{
    ObservableCollection<Person> PersonsList {get; set;}
}

And I don't want to keep the State and City lists in this class. 而且我不想在该课程中保留州和城市列表。 And on changing State I should get Cities combobox populated properly from the enum. 在更改状态时,应从枚举中正确填充“城市”组合框。

I have enums for State and City. 我有州和市的枚举。

1 enum for States which would have state names (say n states). 1个具有州名(例如n个州)的州的枚举。 Then I have n no. 然后我没有。 of Cities enums 城市枚举

What could be the best way of doing it so that I can avoid keeping lists in the Person class following MVVM (ie I don't want to have any code behind). 这样做的最好方法是什么,这样我就可以避免在MVVM之后将列表保留在Person类中(即,我不想后面有任何代码)。

I am new to SO, please let me know if my question is not clear or I am not able to ask the question in a correct way. 我是新来的人,如果我的问题不清楚或无法以正确的方式提出问题,请告诉我。

在VM中保留城市和州的ID,将组合绑定到将从枚举生成的名称-值对列表,然后将其选择的值设置为Person的城市ID /州ID。

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

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