zoukankan      html  css  js  c++  java
  • WPF中ComboBox控件的SelectedItem和SelectedValue的MVVM绑定

    问题描述:左侧是一个ListView控件,用于显示User类的Name属性,右侧显示其SelectedItem的其他属性,包括Age, Address,和Category。其中Category用ComboBox表示。在下拉框选中其他category,可以更改User的属性值。

     

     

     如果Category是string类型,即User类的定义如下

    public class User
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string category { get; set; }
            public string Address { get; set; }
        }
    User Class

    那么,combobox的绑定代码如下:

    <ComboBox Width="150" Canvas.Left="80" ItemsSource="{Binding Path=Categories}" 
                              SelectedItem="{Binding ElementName=listview, Path=SelectedItem.category, Mode=TwoWay}" Name="cbotypes"/>

    直接绑定到了SelectedItem属性。

    如果Category是个复合类型,即User类和Category类定义如下:

    public class User
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public  Category category { get; set; }
            public string Address { get; set; }
        }
    
        public class Category
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
    Category&User class

    那么,combobox的绑定代码如下:

    <ComboBox Width="150" Canvas.Left="80" ItemsSource="{Binding Path=Categories}" DisplayMemberPath="Name"
                              SelectedItem="{Binding ElementName=listview, Path=SelectedItem.category, Mode=TwoWay}" SelectedValuePath="Name" 
                              SelectedValue="{Binding ElementName=listview, Path=SelectedItem.category.Name}" Name="cbotypes"/>

    使用DisplayMemberPath指定了绑定到Category类中的Name属性,并使用了SelectedValue和SelectedValuePath绑定到具体的SelctedItem.

    全部源代码:

    https://github.com/Larissa1990/WPF_ComboBox_SelectedItem

  • 相关阅读:
    Apache ab 压力并发测试工具
    php面试题五之nginx如何调用php和php-fpm的作用和工作原理
    你确定你真的懂Nginx与PHP的交互?
    Linux基本的操作
    【阿里巴巴:高并发的背后】数据库规范
    str()函数
    zfill()方法
    upper()方法
    translate()方法
    title()方法
  • 原文地址:https://www.cnblogs.com/larissa-0464/p/13190446.html
Copyright © 2011-2022 走看看