繁体   English   中英

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

[英]How to refresh combobox after changing selection in another combobox in WPF

我必须在我的窗口组合框:

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

我想要在第一个组合框中选择一个值时,第二个仅由所选项目中的项目填充。 我使用以下方法获得这些物品:

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

与其手动设置每个组合框的数据上下文,不如将外部组件(通常是窗口或其他“较大”组件)的数据上下文设置为视图模型。 然后,为两个组合框显示两个列表(最好是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));
    }
}

暂无
暂无

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

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