学习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. 决策树算法之ID3与C4.5的理解与实现

    github:代码实现 本文算法均使用python3实现 1. 决策树   决策树(decision tree)是一种基本的分类与回归方法(本文主要是描述分类方法),是基于树结构进行决策的,可以将其认 ...

  2. 为什么我要放弃javaScript数据结构与算法(第十一章)—— 算法模式

    本章将会学习递归.动态规划和贪心算法. 第十一章 算法模式 递归 递归是一种解决问题的方法,它解决问题的各个小部分,直到解决最初的大问题.递归通常涉及函数调用自身. 递归函数是像下面能够直接调用自身的 ...

  3. 前端chrome调试技巧

    待更新:http://blog.csdn.net/xueer767/article/details/65936204?locationNum=8&fps=1

  4. BZOJ2600_ricehub_KEY

    题目传送门 这道题一开始我还以为是贪心,sort一遍直接取中点然后求最优值. 但写了之后才发现错误,设置的谷仓只要是一段区间的中点即可.这段区间的两端一定是两片谷田. 所以枚举区间的左端点,二分右端点 ...

  5. 【NAS】CIFS用户场景需求分析

    1.everyone用户 1.1: 场景描述:共享目录为rule,所有用户都可以查看,但是不能修改: 解决方法:在smb.conf里配置read only = yes,具体示例如下: [rule] p ...

  6. androd hook acitivity 启动流程,替换启动的activity(Android Instrumentation)

    前言:如果程序想要知道有activity启动,如果想要拦截activity,然后跳转到指定的activity怎么办? 我们看下ActivityThread 里面: private Activity p ...

  7. Filter配置多个url-pattern

    java开发中会用的Filter过滤器,有时候开发需要,在一个Filter中需要配置多个过滤地址,即<url-pattern>,下面就说一下一个Filter过滤器中多个<url-pa ...

  8. Android 模拟器 下载、编译及调试

    Android 模拟器源码下载 Android 模拟器源码的下载与 Android AOSP 源码库的下载过程类似,可以参考 Google 官方提供的 Android 源码下载文档 来了解这个过程. ...

  9. windows安装logstash-input-jdbc并使用其导入MMSQL数据

    1.安装logstash 2.修改logstash 文件夹下Gemfile文件 将source改为:https://gems.ruby-china.org 3.进入bin目录 执行logstash-p ...

  10. 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)

    题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...