(WPF) MVVM: ComboBox Binding, XML 序列化
基本思路还是在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 = },
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 序列化的更多相关文章
- (WPF, MVVM) Slider Binding.
对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Even ...
- (WPF, MVVM) Textbox Binding
参考:http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger(v=vs.110).aspx Te ...
- (WPF) MVVM: DataGrid Binding
Binding到DataGrid的时候,需要用到ObservableCollection. public ObservableCollection<Customer> Customers ...
- WPF XML序列化保存数据 支持Datagrid 显示/编辑/添加/删除数据
XML序列化保存数据 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- wpf mvvm使用问题集锦
问题一.usercontrol1控件使用了mvvm数据绑定,usercontrol2也使用了mvvm数据绑定,则 以下是伪代码 <usercontrol2 datacontent="{ ...
- A WPF/MVVM Countdown Timer
Introduction This article describes the construction of a countdown timer application written in C# ...
- 使用Prism提供的类实现WPF MVVM点餐Demo
使用Prism提供的类实现WPF MVVM点餐Demo 由于公司开发的技术需求,近期在学习MVVM模式开发WPF应用程序.进过一段时间的学习,感受到:学习MVVM模式,最好的方法就是用MVVM做几个D ...
- WPF MVVM从入门到精通3:数据绑定
原文:WPF MVVM从入门到精通3:数据绑定 WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM从入门到精通3:数据绑定 WPF ...
- C# WPF - MVVM实现OPC Client管理系统
前言 本文主要讲解采用WPF MVVM模式设计OPC Client的过程,算作对于WPF MVVM架构的学习记录吧!不足之处请不吝赐教,感谢! 涉及知识点 C#基础 Xaml基础 命令.通知和数据绑定 ...
随机推荐
- NOIP2011 普及組 統計單詞數
题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...
- 15款Chrome浏览器插件让设计师告别拖延症
秋高气爽,分享一大波有效帮助设计师提高工作效率的Chrome浏览器扩展程序! 高效是另一种王道 无论是在工作中,还是在生活中,有些词我们说来就满满正能量,而另外一些话提起就很沮丧,后者如拖延症,前者如 ...
- dll--二进制层面的复用
积木式思想其实是很自然的一个过程,从c的库函数到C++的标准库,再到dll.com.com+都是这种思想推动下的结果,和现实生活中的人们的思维方式并无二致,只不过软件是在一个虚拟的世界中,并分化出许多 ...
- php安全的改进以及检测文件是否被篡改
可以使用svn的check for modifications//查看更改 至于缓存的php模板文件 可以查找以下执行命令的函数是否存在phpinfo,eval,exec,passthru,shell ...
- 原版本的jquery 开发过程中发现jquery好像更新了
/*! jQuery v1.7.1 jquery.com | jquery.org/license */ (function(a,b){function cy(a){return f.isWindow ...
- C++ map详解
1.什么是mapmap是一个键值对容器.在处理一对一数据是,很有用. 2.map数据结构的特点map内部自建一颗红黑树,这棵树具有对数据自动排序的功能,因此,map内的数据都是按key的值排好序的. ...
- ABBYY FineReader无法打开TWAIN源怎么办
ABBYY FineReader OCR文字识别软件不仅可以将PDF文档和图像文件(包括数码照片)转换为可编辑.可搜索的格式,还可以用来扫描文档,但在扫描过程中,有时可能会出现以下两种错误信息:一是无 ...
- ABBYY将JPEG文件转换成Word文档的方法
日常工作中处理JPEG格式的图像文件时,有时需要转换成Word文档进行编辑,市场上应用而生了很多转换工具,相信不少人听说过OCR(光学字符识别)软件,可以用来转换图像文件,而在OCR软件中, ABBY ...
- Partial RenderPartial Action RenderAction 区别和用法
区别: 1.Partial 与 RenderPartial 两个方法性质基本一样,只是把一个静态用户控件给嵌入进来. 2.Partial 回传一堆html代码,直接写进到页面上@Html.Partia ...
- 原生js的String类扩展
文章转自:http://www.cnblogs.com/zfc2201/archive/2012/12/16/2820335.html JS String类拓展方法: //获取字符数组 String. ...