能够级联两个ComboBox,需要级联一个第三个ComboBox

时间:2017-09-28 15:39:20

标签: c# wpf mvvm data-binding combobox

我需要使用数据绑定创建3个级联组合框(A,B和C)。从我看到的例子中我可以得到两个工作(A和B),但我不知道如何连接ComboBox C,其ItemsSource应该依赖于ComboBox B中的选择。所以,就像对于ComboBox A中显示的每个类别,在ComboBox B中显示一组原因,我需要在ComboBox C中显示ComboBox B中显示的每个原因的一组SubReasons。随着选择的更改,基础下拉列表值也会更改。我希望这是有道理的。有没有办法绑定到ViewModel属性来实现这个或???。

  

XAML

     <ComboBox x:Name="cbxA"
                ItemsSource="{Binding ProjectCategoryList}" 
                SelectedIndex="{Binding Path=SelectedProject.Category}"                      
                DisplayMemberPath="Category"/>
     <ComboBox x:Name="cbxB"
                ItemsSource="{Binding ElementName=cbxA, Path=SelectedItem.Reasons}" 
                Text="{Binding Path=SelectedProject.Reason}" DisplayMemberPath="Name"/>
     <ComboBox x:Name="cbxC"
                ItemsSource="{Binding ElementName=cbxB, Path=SelectedItem.SubReasons}" 
                Text="{Binding Path=SelectedProject.SubReason}"/>
  

视图模型

    private Project selectedProject;

    public Project SelectedProject
    {
        get { return selectedProject; }
        set
        {
            if(selectedProject != value)
            {
                selectedProject = value;
                NotifyPropertyChanged("SelectedProject");
            }
        }
    }

    public List<ProjectCategory> ProjectCategoryList
    {
        get
        {
            return new List<ProjectCategory>()
            {
                new ProjectCategory
                {
                    Category = "One",
                    Reasons = new List<Reason>
                    {
                        new Reason() { Name = "A", SubReasons = new List<String> { "1", "2", "3" }},
                        new Reason() { Name = "B", SubReasons = new List<String> { "4", "5", "6" }},
                        new Reason() { Name = "C", SubReasons = new List<String> { "7", "8", "9" }}, 

                    }
                },
                new ProjectCategory
                {
                    Category = "Two",
                    Reasons = new List<Reason>
                    {
                        new Reason() { Name = "D", SubReasons = Enumerable.Empty<string>()},
                        new Reason() { Name = "E", SubReasons = Enumerable.Empty<string>()},
                        new Reason() { Name = "F", SubReasons = Enumerable.Empty<string>()},
                    }
                },
                new ProjectCategory
                {
                    Category = "Three",
                    Reasons = new List<Reason>
                    {
                        new Reason() { Name = "J", SubReasons = Enumerable.Empty<string>()},
                        new Reason() { Name = "K", SubReasons = Enumerable.Empty<string>()},
                    }
                },
            };
        }
    }
  

ProjectCategory.cs

public class ProjectCategory
{
    public string Category { get; set; }
    public IEnumerable<Reason> Reasons { get; set; }
}

public class Reason
{
    public string Name { get; set; }
    public IEnumerable<string> SubReasons { get; set; }
}

1 个答案:

答案 0 :(得分:1)

Reasons类的ProjectCategory属性应返回IEnumerable<Reason>而不是IEnumerable<string>,而Reason类应包含Name类(或Reasonstring类型的属性和IEnumerable<string>个子原因。

然后你可以用同样的方式绑定cbxC

 <ComboBox x:Name="cbxA"
            ItemsSource="{Binding ProjectCategoryList}" 
            SelectedIndex="{Binding Path=SelectedProject.Category}"                      
            DisplayMemberPath="Category"/>
 <ComboBox x:Name="cbxB"
            ItemsSource="{Binding ElementName=cbxA, Path=SelectedItem.Reasons}" 
            DisplayMemberPath="Name" />
 <ComboBox x:Name="cbxC"
      ItemsSource="{Binding ElementName=cbxb, Path=SelectedItem.SubReasons}" />
public class ProjectCategory
{
    public string Category { get; set; }
    public IEnumerable<Reason> Reasons { get; set; }
}

public class Reason
{
    public string Name { get; set; }
    public IEnumerable<string> SubReasons { get; set; }
}