zoukankan      html  css  js  c++  java
  • 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 多谢~

  • 相关阅读:
    Thinkphp 模板中使用自定义函数的方法
    thinkphp 邮件发送
    str_replace使用
    SQL备份一张表的数据
    error: Allowed memory size
    LitJson使用
    implode,explode的使用
    ModelState.AddModelError使用
    HTTP 错误 404.2
    验证码显示不出来,在THINKPHP中的使用
  • 原文地址:https://www.cnblogs.com/luohengstudy/p/3069466.html
Copyright © 2011-2022 走看看