• WPF 下实现两个ComboBox的MasterDetail 级联 联动 绑定


    要绑定的数据结构如下,一个Category包含多个SubCategory:
    public class Category
        {
            public string Name { get; set; }
            public ObservableCollection<SubCategory> SubCategories { get; set; }
        }
    
        public class SubCategory
        {
            public int Id { get; set; }
            public string SubCategoryName { get; set; }
        }
    

    Code-behind初始化测试数据:

    View Code 
    
        public partial class AssetEditor : Window
        {
            public ObservableCollection<Category> CategorieCollection { get; set; }
    
            public AssetEditor()
            {
                InitializeComponent();
                CategorieCollection = new ObservableCollection<Category>();
                CategorieCollection.Add(new Category()
                {
                    Name = "Cate1",
                    SubCategories = new ObservableCollection<SubCategory>()
                });
    
                CategorieCollection.Add(new Category()
                {
                    Name = "Cate2",
                    SubCategories = new ObservableCollection<SubCategory>()
                });
                CategorieCollection[0].SubCategories.Add(new SubCategory()
                {
                    Id = 0,
                    SubCategoryName = "sub1"
                });
                CategorieCollection[1].SubCategories.Add(new SubCategory()
                {
                    Id = 0,
                    SubCategoryName = "sub2"
                });
    
                cmbCategoryName.DataContext = CategorieCollection;
            }
        }
    

    XAML里绑定:

    <ComboBox Grid.Row="0" Grid.ColumnSpan="2" Grid.Column="1" Name="cmbCategoryName" 
                          ItemsSource="{Binding}" 
                          DisplayMemberPath="Name" 
                          SelectedValuePath="Name" 
                          SelectedItem="{Binding Path=SubCategories}" />
                
                <ComboBox Grid.Row="0" Grid.Column="5" Grid.ColumnSpan="3" Name="cmbSubCategory"
                 DataContext="{Binding ElementName=cmbCategoryName,Path=SelectedItem,Mode=OneWay}" 
                          ItemsSource="{Binding Path=SubCategories, Mode=OneWay}" 
                          DisplayMemberPath="SubCategoryName" 
                          SelectedValuePath="Id" />
    

    来源:http://www.cnblogs.com/polymorphism/archive/2013/01/20/WPF_ComboBox_Master-Detail_Binding.html 多谢~

  • 相关阅读:
    JS调试时返回结果有内容却显示数组长度为0或对象内容为空
    python模块学习之six模块
    python学习之ansible api
    python模块学习之collections
    python模块学习之json
    Tomcat闲聊第二话
    HTTP解读
    实用的工具
    mysql数据库记录
    python模块学习之__future__
  • 原文地址:https://www.cnblogs.com/luohengstudy/p/3069466.html
Copyright © 2020-2023  润新知