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

jomsk1e

我想在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;
    }
}
悠洲

问题在这里

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当下拉菜单在另一个组合框上关闭时,如何更新 WPF 组合框?

来自分类Dev

从服务填充WPF组合框

来自分类Dev

WPF MVVM组合框IsEditable =“ True”

来自分类Dev

WPF MVVM 组合框 SelectionChanged/SelectedItem

来自分类Dev

WPF MVVM:整数的组合框高度不同

来自分类Dev

使用SQL列填充WPF组合框

来自分类Dev

使用SQL列填充WPF组合框

来自分类Dev

在另一个组合框的selectedindexchanged事件上填充一个组合框

来自分类Dev

WPF组合框问题

来自分类Dev

WPF组合框值

来自分类Dev

尝试根据选择另一个组合框填充两个组合框

来自分类Dev

WPF-在第一个组合框选择上启用第二个组合框

来自分类Dev

组合框到另一个组合框

来自分类Dev

根据在Wordpress中另一个组合框中的选择填充组合框

来自分类Dev

C# 使用基于另一个组合框的 XML 数据填充组合框

来自分类Dev

根据选择的另一个组合框填充组合框 - Powershell

来自分类Dev

将多个 WPF 用户定义的组合框绑定到同一个 Observable 集合

来自分类Dev

当它是前一个项目的前缀时,如何在 wpf 组合框中选择一个项目?

来自分类Dev

在JS中用另一个组合值填充组合框值

来自分类Dev

另一个组合框内的组合框

来自分类Dev

WPF多列组合框

来自分类Dev

清除WPF中的组合框

来自分类Dev

wpf datagrid组合框列

来自分类Dev

WPF组合框验证规则

来自分类Dev

WPF多列组合框

来自分类Dev

wpf datagrid组合框列

来自分类Dev

WPF组合框未绑定

来自分类Dev

WPF-MVVM:SelectionChanged之后的组合框值

来自分类Dev

WPF MVVM中组合框的默认值