基本思路还是在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 序列化的更多相关文章

  1. (WPF, MVVM) Slider Binding.

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

  2. (WPF, MVVM) Textbox Binding

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

  3. (WPF) MVVM: DataGrid Binding

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

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

    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/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# WPF - MVVM实现OPC Client管理系统

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

随机推荐

  1. smartUpload组件文件上传

    public class SmartUploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, ...

  2. 关于JSP的C标签之forEach循环分隔符

    页面中可能说出现在forEach循环中间需要出力分隔符的问题, 比如: 小明 1年级,小王 2年级, 小张 3年级(循环单位是 ${bean.name} ${bean.class}) 此时的逗号出力, ...

  3. leetcode 144. Binary Tree Preorder Traversal ----- java

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  4. 学号20145220 《Java程序设计》第5周学习总结

    学号20145220 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承结构 8.1.1使用try.catch java中所有的错误都会被打包为对象,并提供了特有的语句进行处 ...

  5. URAL 1208 Legendary Teams Contest(DFS)

    Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A l ...

  6. Python爬虫学习笔记——防豆瓣反爬虫

    开始慢慢测试爬虫以后会发现IP老被封,原因应该就是单位时间里面访问次数过多,虽然最简单的方法就是降低访问频率,但是又不想降低访问频率怎么办呢?查了一下最简单的方法就是使用转轮代理IP,网上找了一些方法 ...

  7. eclipse adt 项目依赖,使用git上的项目

    从git下载的android工程不能直接导入eclipse,有的项目要依赖这个项目,怎么办呢 好像只能新建一个android项目,然后把libs,res,src,AndroidManifest.xml ...

  8. (转) SLAM系统的研究点介绍 与 Kinect视觉SLAM技术介绍

          首页 视界智尚 算法技术 每日技术 来打我呀 注册     SLAM系统的研究点介绍 本文主要谈谈SLAM中的各个研究点,为研究生们(应该是博客的多数读者吧)作一个提纲挈领的摘要.然后,我 ...

  9. Java theory and practice

    This content is part of the series: Java theory and practice A brief history of garbage collection A ...

  10. Useful SQL Server Article

    http://blogs.technet.com/b/topsupportsolutions/archive/2013/11/06/top-support-solutions-for-microsof ...