(WPF) MVVM: ComboBox Binding

基本思路还是在View的Xmal里面绑定ViewModel的属性,虽然在View的后台代码中也可以实现binding,但是还是在Xmal里面相对的代码量要少一些。

此例子要实现的效果就是将一个List<Customer> 绑定到一个ComboBox,并将选择后的Customer的Age显示在一个TextBlock中。

1. Model

    public class Customer
    {
        public string Name
        {
            get;
            set;
        }

        public int Age
        {
            get;
            set;
        }
    }

2. ViewModel

    public class CustomerViewModel : ViewModelBase
    {
        private List<Customer> customers;

        private Customer selectedCustomer; 

        public CustomerViewModel()
        {
            this.customers = new List<Customer>()
            {
                new Customer { Name = "Paul", Age = 28 },
                new Customer { Name = "Fred", Age = 37 },
                new Customer { Name = "Cherry", Age = 21 },
            };

            this.selectedCustomer = new Customer();
        }

        public List<Customer> Customers
        {
            get
            {
                return this.customers;
            }
            set
            {
                if (!this.customers.Equals(value))
                {
                    this.customers = value;
                    base.OnPropertyChanged("Customers");
                }
            }
        }

        public Customer SelectedCustomer
        {
            get
            {
                return this.selectedCustomer;
            }
            set
            {
                if (!this.selectedCustomer.Equals(value))
                {
                    this.selectedCustomer = value;
                    base.OnPropertyChanged("SelectedCustomer");
                }
            }
        }
    }

3. View.

<UserControl x:Class="WpfApplication1.View.CustomerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vm="clr-namespace:WpfApplication1.ViewModel"
             mc:Ignorable="d"
             Height="308.072"
             Width="457.399">
    <UserControl.DataContext>
        <vm:CustomerViewModel/>
    </UserControl.DataContext>
    <Grid>
        <ComboBox HorizontalAlignment="Left"
                  Margin="45,47,0,0"
                  VerticalAlignment="Top"
                  Width="120"
                  Height="31"
                  ItemsSource="{Binding Customers}"
                  SelectedItem="{Binding SelectedCustomer}"
                  DisplayMemberPath="Name"/>
        <TextBlock HorizontalAlignment="Left"
                   Margin="212,52,0,0"
                   TextWrapping="Wrap"
                   Text="{Binding SelectedCustomer.Age}"
                   VerticalAlignment="Top"
                   Height="26"
                   Width="101" />

    </Grid>
</UserControl>

还有其他供选择的binding方式如下:

    <TextBlock Text="Example 1" VerticalAlignment="Center"/>
            <ComboBox ItemsSource="{Binding MyStringOptions}" Grid.Column="1" SelectedItem="{Binding SelectedOption1}" Margin="5"/>
            <TextBlock Text="{Binding SelectedOption1}" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"/>

            <TextBlock Grid.Row="1" Text="Example 2" VerticalAlignment="Center"/>
            <ComboBox Grid.Row="1" Grid.Column="1" ItemsSource="{Binding MyClassOptions}" SelectedItem="{Binding SelectedClass}" DisplayMemberPath="Name" Margin="5"/>
            <TextBlock Grid.Row="1" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"><Run Text="{Binding SelectedClass.Name}"/><Run Text=" - "/><Run Text="{Binding SelectedClass.Age}"/></TextBlock>

            <TextBlock Grid.Row="2" Text="Example 3" VerticalAlignment="Center"/>
            <ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding MyClassOptions}" SelectedValuePath="Age" SelectedValue="{Binding SelectedAge}" DisplayMemberPath="Name" Margin="5"/>
            <TextBlock Grid.Row="2" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"><Run Text="Selected age: "/><Run Text="{Binding SelectedAge}"/></TextBlock>

            <TextBlock Grid.Row="3" Text="Example 4" VerticalAlignment="Center"/>
            <ComboBox Grid.Row="3" Grid.Column="1" ItemsSource="{Binding MyClassOptions}" SelectedValuePath="Age" SelectedValue="{Binding SelectedAge}" ItemTemplate="{StaticResource Example4ItemTemplate}" Margin="5"/>
            <TextBlock Grid.Row="3" Grid.Column="2" Margin="10,5,5,0" VerticalAlignment="Center"><Run Text="Selected age: "/><Run Text="{Binding SelectedAge}"/></TextBlock>

(WPF) MVVM: ComboBox Binding,布布扣,bubuko.com

时间: 2024-08-08 22:07:20

(WPF) MVVM: ComboBox Binding的相关文章

(WPF) MVVM: DataGrid Binding

Binding到DataGrid的时候,需要用到ObservableCollection. public ObservableCollection<Customer> Customers { get { return this.customers; } set { this.customers = value; base.OnPropertyChanged("Customers"); } } (WPF) MVVM: DataGrid Binding,布布扣,bubuko.c

(WPF, MVVM) Slider Binding.

对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Event进行绑定,如何进行呢? (对于UIElement的一些Event进行绑定一定有一些通用的方法,目前还没有深入研究.) 首先,Slider Value的绑定是很简单的, 绑定Slider的Value属性即可. (1)ViewModel public class SliderViewModel : V

WPF - MVVM - 如何将ComboBox的Selectchange事件binding到ViewModel

将所有的事件,属性,都映射到ViewModel中.好处多多,以后开发尽量用这种模式. 解决方法: 使用System.Windows.Interactivity.dll,添加该dll到项目引用 ? 1 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" ComboBox映射的代码: <ComboBox VerticalAlignment="Ce

A WPF/MVVM Countdown Timer

Introduction This article describes the construction of a countdown timer application written in C# and WPF, using Laurent Bugnion's MVVMLight Toolkit. This article is based on the work of some previous articles I've written: A WPF Short TimeSpan Cus

WPF的ComboBox 数据模板自定义

WPF的ComboBox 有些时候不能满足用户需求,需要对数据内容和样式进行自定义,下面就简要介绍一下用数据模板(DataTemplate)的方式对ComboBox 内容进行定制: 原型设计如下: 步骤: 1.新建一个WPF应用程序WpfAppDemo(VS2012),并新建一个images文件夹(上传图片素材); 2.在主界面MainWindow.xaml文件中添加一个Label.ComboBox 和Button控件,如下图: 代码如下: 1 <Window x:Class="WpfAp

WPF MVVM初体验

首先MVVM设计模式的结构, Views: 由Window/Page/UserControl等构成,通过DataBinding与ViewModels建立关联: ViewModels:由一组命令,可以绑定的属性,操作逻辑构成:因为View与ViewModel进行了解耦,我们可以对ViewModel进行Unit Test: Models:可以是实体对象或者Web服务: 下面通过一个简单的例子,来介绍一些WPF MVVM模式.示例将展示一个图片浏览器,打开图片,放大/缩小图片大小.首先项目结构: UI

转载:WPF MVVM之INotifyPropertyChanged接口的几种实现方式

原文地址:http://www.cnblogs.com/xiwang/ 序言 借助WPF/Sliverlight强大的数据绑定功能,可以比实现比MFC,WinForm更加优雅轻松的数据绑定.但是在使用WPF/Silverlight绑定时,有件事情是很苦恼的:当ViewModel对象放生改变,需要通知UI.我们可以让VM对象实现INotifyPropertyChanged接口,通过事件来通知UI.但问题就出现这里…… 一,描述问题 情形:现在需要将一个Person对象的Name熟悉双向绑定到UI中

WPF MVVM TreeView 实现 右键选中 右键菜单

1.非MVVM模式:下载源代码WpfApplication1.zip <TreeView Height="200" PreviewMouseRightButtonDown="TreeViewItem_PreviewMouseRightButtonDown" HorizontalAlignment="Left" Margin="12,0,0,0" Name="treeView1" VerticalAli

WPF MVVM+EF增删改查 简单示例(二) 1对1 映射

WPF MVVM+EF增删改查 简单示例(一)实现了对学生信息的管理. 现在需求发生变更,在录入学生资料的时候同时需要录入学生的图片信息,并且一名学生只能有一张图片资料.并可对学生的图片资料进行更新. 添加了那些功能,先看看效果图: 第一步:添加实体类StudentPhotoEntity.cs public class StudentPhotoEntity { public int StudentId { get; set; } public byte[] StudentPhoto { get;