如何在 WPF 的另一个组合框中更改选择后刷新组合框

How to refresh combobox after changing selection in another combobox in WPF

我必须在 window:

中组合框
<ComboBox x:Name="cbDeps"
        ItemsSource="{Binding}" DisplayMemberPath="sName" SelectedValuePath="nDepartmentIdn"
        Grid.Row="0" Margin="0,52,215,0" Grid.Column="1" SelectionChanged="cbUsers_SelectionChanged"/>
<ComboBox x:Name="cbUsersDep"
        ItemsSource="{Binding}" DisplayMemberPath="sUserName" SelectedValuePath="nUserIdn"
        Grid.Row="0" Margin="218,52,0,0" Grid.Column="1"/>

并且我希望当我 select 第一个组合框中的值时,第二个仅由 selected 项目的项目填充。我使用这种方法得到这些项目:

public ObservableCollection<DataModel.TB_USER> listUsersParDeps(int numDep)
{
    var userDeps = (from DataModel.TB_USER ud in SessionContext.DBContext.TB_USER
                    where ud.nDepartmentIdn==numDep
                    select ud);

    foreach (var userDep in userDeps)
    {
        userM.ListUserDep.Add(userDep);
    }

    return userM.ListUserDep;
}

我把它们放在数据上下文中:

cbUsersDep.DataContext = userVM.listUsersParDeps(int.Parse(cbDeps.SelectedValue.ToString()));

与其手动设置每个组合框的数据上下文,不如设置外部组件的数据上下文(通常是window,或者其他一些“更大” 组件)到你的视图模型。然后为两个组合框公开两个列表(最好是 ObservableCollection),并将组合框的 ItemSource 属性绑定到这些列表。然后,每当第一个组合框的 SelectedItem 发生变化时,您就会更新第二个列表:

<StackPanel>
    <ComboBox ItemsSource="{Binding Departments}" SelectedItem="{Binding SelectedDepartment}" />
    <ComboBox ItemsSource="{Binding Users}" />
</StackPanel>
public class MainViewModel : INotifyPropertyChanged
{
    // this holds the data
    private Dictionary<string, List<string>> departmentUsers = new Dictionary<string,List<string>>();

    private List<string> departments;
    public List<string> Departments
    {
        get { return departments; }
        set
        {
            if (value != departments)
            {
                departments = value;
                OnNotifyPropertyChanged("Departments");
            }
        }
    }

    private string selectedDepartment;
    public string SelectedDepartment
    {
        get { return selectedDepartment; }
        set
        {
            if (value != selectedDepartment)
            {
                selectedDepartment = value;
                OnNotifyPropertyChanged("SelectedDepartment");

                // update users list
                Users = departmentUsers[selectedDepartment];
            }
        }
    }

    private List<string> users;
    public List<string> Users
    {
        get { return users; }
        set
        {
            if (value != users)
            {
                users = value;
                OnNotifyPropertyChanged("Users");
            }
        }
    }

    public MainViewModel()
    {
        // sample data
        departmentUsers = new Dictionary<string, List<string>>();
        departmentUsers.Add("Department 1", new List<string>() { "1.1", "1.2", "1.3" });
        departmentUsers.Add("Department 2", new List<string>() { "2.1", "2.2", "2.3", "2.4", "2.5" });
        departmentUsers.Add("Department 3", new List<string>() { "3.1", "3.2" });
        departmentUsers.Add("Department 4", new List<string>() { "4.1", "4.2", "4.3" });

        // initial state
        Departments = new List<string>(departmentUsers.Keys);
        SelectedDepartment = Departments[0];
    }

    // simple INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnNotifyPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}