(WPF) MVVM: ComboBox Binding, XML 序列化

时间:2022-01-31 05:07:25

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

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

(WPF) MVVM: ComboBox Binding, XML 序列化

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 = },
new Customer { Name = "Fred", Age = },
new Customer { Name = "Cherry", Age = },
}; 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=""
Height=""
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=""
Width="" /> </Grid>
</UserControl>

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

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

再深入一步,在实际的程序中,是务必要减少那些Hardcode的,所以我们可以把数据存放在一个单独的xml文件中。

并通过对xml的文件的序列化解析,正确的获取里面的数据。

另外,还可以binding ComboBox 到 enum 和 dictionary

绑定到 Enum

http://blog.163.com/cloud_thegreat/blog/static/10367215620115233941346/

(WPF) MVVM: ComboBox Binding, XML 序列化的更多相关文章

  1. &lpar;WPF&comma; MVVM&rpar; Slider Binding&period;

    对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Even ...

  2. &lpar;WPF&comma; MVVM&rpar; Textbox Binding

    参考:http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger(v=vs.110).aspx Te ...

  3. &lpar;WPF&rpar; MVVM&colon; DataGrid Binding

    Binding到DataGrid的时候,需要用到ObservableCollection. public ObservableCollection<Customer> Customers ...

  4. WPF XML序列化保存数据 支持Datagrid 显示&sol;编辑&sol;添加&sol;删除数据

    XML序列化保存数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  5. wpf mvvm使用问题集锦

    问题一.usercontrol1控件使用了mvvm数据绑定,usercontrol2也使用了mvvm数据绑定,则 以下是伪代码 <usercontrol2 datacontent="{ ...

  6. A WPF&sol;MVVM Countdown Timer

    Introduction This article describes the construction of a countdown timer application written in C# ...

  7. 使用Prism提供的类实现WPF MVVM点餐Demo

    使用Prism提供的类实现WPF MVVM点餐Demo 由于公司开发的技术需求,近期在学习MVVM模式开发WPF应用程序.进过一段时间的学习,感受到:学习MVVM模式,最好的方法就是用MVVM做几个D ...

  8. WPF MVVM从入门到精通3:数据绑定

    原文:WPF MVVM从入门到精通3:数据绑定   WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM从入门到精通3:数据绑定 WPF ...

  9. C&num; WPF - MVVM实现OPC Client管理系统

    前言 本文主要讲解采用WPF MVVM模式设计OPC Client的过程,算作对于WPF MVVM架构的学习记录吧!不足之处请不吝赐教,感谢! 涉及知识点 C#基础 Xaml基础 命令.通知和数据绑定 ...

随机推荐

  1. 『&period;NET Core CLI工具文档』(十四)dotnet-install 脚本参考

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-install scripts reference 翻译:dotnet-install 脚本参考 名称 d ...

  2. IIS 7&period;0 部署MVC

    开发的MVC 3.0 项目,在部署服务上还是与需要花一点功夫,这里把遇到的问题罗列出来. 本文主要介绍IIS 7.5中安装配置MVC 3.0的具体办法! 部署必备: Microsoft .net Fr ...

  3. Debian 8下vsftpd安装与配置

    Debian 8下vsftpd安装与配置 0.环境 root@remote:/# uname -r 3.16.0-4-amd64 root@remote:/e# lsb_release No LSB ...

  4. 献给所有从事IT行业拥有梦想的英语渣们

    本文适合读者:从小到大英语都不好,如今选择IT行业需要重拾英语追寻梦想的人,英语大神们请绕行.. 目录 一.背景介绍 二.明确学习目标 2.1 英文文法 2.2 词汇量 三.题外话 3.1 关于本文 ...

  5. 【Java 基础篇】【第四课】初识类

    看看Java中如何定义一个类,然后用来调用的,这个比较简单,直接看代码吧. 我发现的类和C++不一样的地方: 1.Java中类定义大括号后没有分号: 2.好像没有 public.private等关键字 ...

  6. ubuntu 换源

    经过自己一番折腾后,发现用这个方法换源最简单.直接. sudo vi /etc/apt/sources.list 把sources.list里面的内容全部替换为一下内容 deb http://mirr ...

  7. hadoop mapreduce 端参数优化

    在MapReduce执行过程中,特别是Shuffle阶段,尽量使用内存缓冲区存储数据,减少磁盘溢写次数:同时在作业执行过程中增加并行度,都能够显著提高系统性能,这也是配置优化的一个重要依据. 下面分别 ...

  8. MongoDB对应SQL语句

    -------------------MongoDB对应SQL语句------------------- 1.Create and Alter     1.     sql:         crea ...

  9. 跨域资源共享 CORS 详解(转)

    add by zhj: 公司在一个web产品上,做前后端分离,前后端提供独立的服务,使用不同的域名,通过http进行交互,在 前端,会涉及到跨域访问的问题,前端使用了CORS,后端同时需要做相应修改, ...

  10. 归并排序&lpar;C&plus;&plus;实现&rpar;

         归并排序是利用"归并"技术来进行排序.归并是指将若干个已排序的子文件合并成一个有序的文件.常见的归并排序有两路归并排序(Merge Sort),多相归并排序(Polyph ...