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

时间:2014-03-19 03:34:32

标签: c# wpf mvvm

我想在combobox1选择改变事件后填充我的combobox2。

这是我的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}"/>

我的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));
                }
            }
        }
    }
}

我的问题是我不知道在哪里触发我的PopulateProcess()方法,以便根据combobox1的选择填充我的combobox2。感谢您的所有时间和帮助! :)

- 编辑 -

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;
    }
}

3 个答案:

答案 0 :(得分:3)

最初第二个组合框是空的,并且选择第一个组合框改变后只是瞳孔化过程

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

答案 1 :(得分:2)

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

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));
        }
    }
}

然后将主视图模型绑定到视图的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;
}

在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}" />

答案 2 :(得分:1)

问题在这里

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

应该 WorkcodeId 而不是 WorkcodeID 。休息你可以尝试Nishanth回复

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