学习WVVM模式,设计一个简单的菜单显示和选择时显示个数的一个例子。

最终效果:

所建文件结构如下:

MenuModel:菜品属性-名称和价格

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WpfApplication2.Model
{
public class MenuModel
{
public string Name { get; set; }
public string Price { get; set; } }
}

DelegateCommend:命令属性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input; namespace WpfApplication2.Model
{
public class DelegateCommend:ICommand
{
public Action<object> ExecuteAction { get; set; }
public Func<object,bool> CanExecuteFunc { get; set; }
public bool CanExecute(object parameter)
{
if (CanExecuteFunc==null)
return true;
return CanExecuteFunc(parameter);
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
if (ExecuteAction == null)
return;
ExecuteAction(parameter);
}
}
}

DishService:初始化菜品集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfApplication2.Model; namespace WpfApplication2.Servers
{
public class DishService
{
public List<ListMenuModel> GetDishes()
{
List<ListMenuModel> list = new List<ListMenuModel>();
list.Add(new ListMenuModel {Dishes= new MenuModel{Name = "黄瓜", Price = "" },IsSelected=false});
list.Add(new ListMenuModel { Dishes = new MenuModel { Name = "酸菜", Price = "" }, IsSelected = false });
list.Add(new ListMenuModel { Dishes = new MenuModel { Name = "拉皮", Price = "" }, IsSelected = false });
list.Add(new ListMenuModel { Dishes = new MenuModel { Name = "凉粉", Price = "" }, IsSelected = false });
list.Add(new ListMenuModel { Dishes = new MenuModel { Name = "豆芽", Price = "" }, IsSelected = false });
list.Add(new ListMenuModel { Dishes = new MenuModel { Name = "京皮", Price = "" }, IsSelected = false });
return list;
}
}
}

ListMenuModel:界面中菜品和选择复选框的viewmodel,具有通知功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text; namespace WpfApplication2.Model
{
public class ListMenuModel:INotifyPropertyChanged
{
public MenuModel Dishes { get; set; }
private bool isSelected; public bool IsSelected
{
get { return isSelected; }
set { isSelected = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
} public event PropertyChangedEventHandler PropertyChanged;
}
}

MainViews:界面所有数据绑定的源

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using WpfApplication2.Model;
using WpfApplication2.Servers; namespace WpfApplication2.Views
{
public class MainViews:INotifyPropertyChanged
{
public DelegateCommend SelectCmd { get; set; } private List<ListMenuModel> dishes; public List<ListMenuModel> Dishes
{
get { return dishes; }
set { dishes = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Dishes"));
}
} private int count; public int Count
{
get { return count; }
set { count = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Count"));
}
} public MainViews()
{
Dishes = (new DishService()).GetDishes(); SelectCmd = new DelegateCommend(); SelectCmd.ExecuteAction = x =>
{
this.Count = Dishes.Where(n => n.IsSelected == true).Count();
};
} public event PropertyChangedEventHandler PropertyChanged;
} }

MainWindow.xaml:界面

<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid> <Border CornerRadius="" Background="Yellow" BorderThickness="" BorderBrush="Orange" Margin="0,0,0.4,-0.2" Grid.RowSpan="">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Border BorderThickness="" BorderBrush="Orange" Padding="" CornerRadius="">
<StackPanel>
<StackPanel.Effect>
<DropShadowEffect Color="LightBlue"></DropShadowEffect>
</StackPanel.Effect>
<TextBlock Text="欢迎光临!" FontSize=""></TextBlock>
</StackPanel>
</Border> <DataGrid Name="dishGrid" Grid.Row="" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" GridLinesVisibility="None">
<DataGrid.Columns>
<DataGridTextColumn Header="菜名" Binding="{Binding Dishes.Name}"></DataGridTextColumn>
<DataGridTextColumn Header="价格" Binding="{Binding Dishes.Price}"></DataGridTextColumn>
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}" Command="{Binding DataContext.SelectCmd,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns> </DataGrid> <StackPanel Grid.Row="" HorizontalAlignment="Right" Orientation="Horizontal" Margin="">
<TextBlock Text="共计" FontSize="16 "></TextBlock>
<TextBox IsReadOnly="True" Width="" Text="{Binding Count}"></TextBox>
<Button Content="下单" Height="" Width=""></Button>
</StackPanel>
</Grid>
</Border>
</Grid>
</Window>

其C#代码如下:设置数据及绑定

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication2.Model;
using WpfApplication2.Servers;
using WpfApplication2.Views;
namespace WpfApplication2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); MainViews mw = new MainViews(); this.DataContext = mw;
dishGrid.ItemsSource = mw.Dishes; }
}
}

C#中WVVM的使用的更多相关文章

  1. Python开源框架

    info:更多Django信息url:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC) ...

  2. [UWP]xaml中自定义附加属性使用方法的注意项

    ---恢复内容开始--- 随笔小记,欢迎指正 在UWP平台上做WVVM的时候,想针对ListBox的SelectionChanged事件定义一个自定义的命令,于是使用自定义附加属性的方式.可是最后自定 ...

  3. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  4. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  5. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  6. .NET Core中的认证管理解析

    .NET Core中的认证管理解析 0x00 问题来源 在新建.NET Core的Web项目时选择“使用个人用户账户”就可以创建一个带有用户和权限管理的项目,已经准备好了用户注册.登录等很多页面,也可 ...

  7. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  8. 关于CryptoJS中md5加密以及aes加密的随笔

    最近项目中用到了各种加密,其中就包括从没有接触过得aes加密,因此从网上各种查,官方的一种说法: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学 ...

  9. In-Memory:在内存中创建临时表和表变量

    在Disk-Base数据库中,由于临时表和表变量的数据存储在tempdb中,如果系统频繁地创建和更新临时表和表变量,大量的IO操作集中在tempdb中,tempdb很可能成为系统性能的瓶颈.在SQL ...

随机推荐

  1. 20154327 Exp5 MSF基础应用

    基础问题回答 用自己的话解释什么是exploit,payload,encode. exploit漏洞利用,一般出现漏洞后,根据一些大佬们给出的POC尝试去进行漏洞利用. payload攻击负载,是我们 ...

  2. 北京Uber优步司机奖励政策(1月11日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 成都Uber优步司机奖励政策(3月27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. 青岛Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. springboot 读写excel

    添加两个坐标: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...

  6. 使用redux-actions优化actions管理

    redux-actions的api很少,有三个createAction(s)  handleASction(s)   combineActions 主要用到createAction去统一管理actio ...

  7. 第3章 TCP协议详解

    第3章 TCP协议详解 3.1 TCP服务的特点 传输协议主要有两个:TCP协议和UDP协议,TCP协议相对于UDP协议的特点是 面向连接使用TCP协议通信的双方必须先建立连接,完成数据交换后,通信双 ...

  8. Python|一文简单看懂 深度&广度 优先算法

    一.前言 以后尽量每天更新一篇,也是自己的一个学习打卡!加油!今天给大家分享的是,Python里深度/广度优先算法介绍及实现. 二.深度.广度优先算法简介 1. 深度优先搜索(DepthFirstSe ...

  9. Oracle作业练习题

    第一问 //登陆scott用户 //解锁 alter user scott account unlock; //给用户申请密码 alter user scott identified by tiger ...

  10. 基础的表ADT -数据结构(C语言实现)

    读数据结构与算法分析 表的概述 形如A1,A2,A3... 操作合集 PrintList MakeEmpty Find Insert Delete 表的简单数组实现 分析: PrintList和Fin ...