WPF MVVM 填充另一个组合框的组合框 OnPropertyChanged

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22495677/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:16:02  来源:igfitidea点击:

WPF MVVM Populate combobox OnPropertyChanged of another combobox

c#wpfmvvm

提问by jomsk1e

I want to populate my combobox2 after combobox1 selection changed event.

我想在组合框 1 选择更改事件后填充组合框 2。

Here's some part of my XAML:

这是我的 XAML 的一部分:

<ComboBox Name="cmbWorkcode"
        ItemsSource="{Binding Workcodes}" 
        DisplayMemberPath="WorkcodeName" 
        SelectedValuePath="WorkcodeID" 
        SelectedValue="{Binding Path=WorkcodeId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<ComboBox Name="cmbProcess" 
        ItemsSource="{Binding Processes}"
        DisplayMemberPath="ProcessName" SelectedValuePath="ProcessId"
        SelectedValue="{Binding Path=ProcessId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Some part of my ViewModel:

我的 ViewModel 的某些部分:

class MainWindowViewModel : ObservableObject 
{
    private ObservableCollection<Workcode> _workcodes = new ObservableCollection<Workcode>();
    public ObservableCollection<Workcode> Workcodes
    {
        get { return _workcodes; }
        set
        {
            _workcodes = value;
            OnPropertyChanged("Workcodes");
        }
    }

    private int _workcodeId;
    public int WorkcodeId
    {
        get { return _workcodeId; }
        set
        {
            _workcodeId = value;
            OnPropertyChanged("WorkcodeId");
        }
    }

    private ObservableCollection<Process> _processes = new ObservableCollection<Process>();
    public ObservableCollection<Process> Processes
    {
        get { return _processes; }
        set
        {
            _processes = value;
            OnPropertyChanged("Processes");
        }
    }

    private int _processId;
    public int ProcessId
    {
        get { return _processId; }
        set
        {
            _processId = value;
            OnPropertyChanged("ProcessId");
        }
    }

    public MainWindowViewModel()
    {
        PopulateWorkcode();
    }

    private void PopulateWorkcode()
    {
        using (var db = new DBAccess())
        {
            db.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
            db.Query = @"SELECT workcodeId, workcode FROM workcode";
            DataTable data = db.GetData();
            if (data != null)
            {
                foreach (DataRow row in data.Rows)
                {
                    int workcodeId = Convert.ToInt32(row["workcodeId"].ToString());
                    string workcodeName = row["workcode"].ToString();
                    _workcodes.Add(new Workcode(workcodeId, workcodeName));
                }
            }
        }    
    }

    private void PopulateProcess()
    {
        using (var db = new DBAccess())
        {
            db.ConnectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
            db.Query = @"SELECT ProcessId, ProcessName FROM `process` WHERE WorkcodeId = @workcodeId";
            DataTable data = db.GetData(new[] {new MySqlParameter("@workcodeId", _workcodeId.ToString())});
            if (data != null)
            {
                foreach (DataRow row in data.Rows)
                {
                    int id = Convert.ToInt32(row["ProcessId"].ToString());
                    string name = row["ProcessName"].ToString();
                    _processes.Add(new Process(id, name));
                }
            }
        }
    }
}

My problem is I don't know where do I trigger my PopulateProcess() method so that my combobox2 will be populated base on the selection of combobox1. Thanks for all the time and help! :)

我的问题是我不知道在哪里触发我的 PopulateProcess() 方法,以便我的组合框 2 将根据组合框 1 的选择进行填充。感谢您一直以来的帮助!:)

--EDIT--

- 编辑 -

public class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Workcode
{
    public int WorkcodeId { get; set; }
    public string WorkcodeName { get; set; }

    public Workcode(int id, string name)
    {
        WorkcodeId = id;
        WorkcodeName = name;
    }
}

回答by Nishanth

initially the second combobox is empty and on select of the first combobox changed just pupulate the process

最初,第二个组合框是空的,在选择第一个组合框时改变了这个过程

 private int _workcodeId;
 public int WorkcodeId
 {
   get { return _workcodeId; }
   set
   {
      _workcodeId = value;
      OnPropertyChanged("WorkcodeId");
      if(WorkcodeID>0) PopulateProcess();
   }
}

回答by Sajeetharan

I can understand you want to have the next combobox to fill with data based on the previous value. Since i don't have classes of your type, i will give a simple example,

我可以理解您希望下一个组合框根据前一个值填充数据。由于我没有您类型的课程,我将举一个简单的例子,

class ItemListViewModel<T> : INotifyPropertyChanged where T : class
{
    private T _item;
    private ObservableCollection<T> _items;

    public ItemListViewModel()
    {
        _items = new ObservableCollection<T>();
        _item = null;
    }

    public void SetItems(IEnumerable<T> items)
    {
        Items = new ObservableCollection<T>(items);
        SelectedItem = null; 
    }

    public ObservableCollection<T> Items
    {
        get { return _items; }
        private set
        {
            _items = value;
            RaisePropertyChanged("Items");
        }
    }

    public T SelectedItem
    {
        get { return _item; }
        set
        {
            _item = value;
            RaisePropertyChanged("SelectedItem");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Then have the main viewmodel that will be bound to the DataContext of the view. Have the Load methods do what you want

然后拥有将绑定到视图的 DataContext 的主视图模型。让 Load 方法做你想做的事

class MyViewModel : INotifyPropertyChanged
{
    public MyViewModel()
    {
        First = new ItemListViewModel<string>();
        Second = new ItemListViewModel<string>();
        Third = new ItemListViewModel<string>();

        First.PropertyChanged += (s, e) => Update(e.PropertyName, First, Second, LoadSecond);
        Second.PropertyChanged += (s, e) => Update(e.PropertyName, Second, Third, LoadThird);

        LoadFirst();
    }

    public ItemListViewModel<string> First { get; set; }
    public ItemListViewModel<string> Second { get; set; }
    public ItemListViewModel<string> Third { get; set; }

    private void LoadFirst()
    {
        First.SetItems(new List<string> { "One", "Two", "Three" });
    }
    private void LoadSecond()
    {
        Second.SetItems(new List<string> { "First", "Second", "Third" });
    }
    private void LoadThird()
    {
         Third.SetItems(new List<string> { "Firsty", "Secondly", "Thirdly" });
    }

    private void Update<T0, T1>(string propertyName, ItemListViewModel<T0> parent, ItemListViewModel<T1> child, Action loadAction)
        where T0 : class
        where T1 : class
    {
        if (propertyName == "SelectedItem")
        {
            if (parent.SelectedItem == null)
            {
                child.SetItems(Enumerable.Empty<T1>());
            }
            else
            {
                loadAction();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

In XAML,

在 XAML 中,

    <ComboBox ItemsSource="{Binding First.Items}" SelectedItem="{Binding First.SelectedItem}" />
    <ComboBox ItemsSource="{Binding Second.Items}" SelectedItem="{Binding Second.SelectedItem}" />
    <ComboBox ItemsSource="{Binding Third.Items}" SelectedItem="{Binding Third.SelectedItem}" />

回答by yo chauhan

The issue is here

问题在这里

<ComboBox Name="cmbWorkcode"
    ItemsSource="{Binding Workcodes}" 
    DisplayMemberPath="WorkcodeName" 
    SelectedValuePath="WorkcodeId" 
    SelectedValue="{Binding Path=WorkcodeId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

It should be WorkcodeIdinstead of WorkcodeID. rest you can try as Nishanth replied

它应该是WorkcodeId而不是WorkcodeID。休息,你可以试试 Nishanth 的回答

public int WorkcodeId
{
   get { return _workcodeId; }
   set
   {
   if(_workcodeId !=value)
  {
    _workcodeId = value;
    OnPropertyChanged("WorkcodeId");
    PopulateProcess();
  }
 }
}