• 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 多谢~

  • 相关阅读:
    ORA-01565 ORA-15025 ORA-27041
    11g r2 vip启动过程
    控制文件多路径复用ORA-00205错误
    sqlmap 整合Meterpreter
    Mataasploit 常见问题
    AWVS的安装、破解与使用
    hydra常见场景
    php 学习笔记
    python学习笔记
    计算机网络基础,网络协议,常见状态码
  • 原文地址:https://www.cnblogs.com/luohengstudy/p/3069466.html
Copyright © 2020-2023  润新知