C#中WVVM的使用
学习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的使用的更多相关文章
- Python开源框架
info:更多Django信息url:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC) ...
- [UWP]xaml中自定义附加属性使用方法的注意项
---恢复内容开始--- 随笔小记,欢迎指正 在UWP平台上做WVVM的时候,想针对ListBox的SelectionChanged事件定义一个自定义的命令,于是使用自定义附加属性的方式.可是最后自定 ...
- mapreduce中一个map多个输入路径
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- .NET Core中的认证管理解析
.NET Core中的认证管理解析 0x00 问题来源 在新建.NET Core的Web项目时选择“使用个人用户账户”就可以创建一个带有用户和权限管理的项目,已经准备好了用户注册.登录等很多页面,也可 ...
- Angular杂谈系列1-如何在Angular2中使用jQuery及其插件
jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...
- 关于CryptoJS中md5加密以及aes加密的随笔
最近项目中用到了各种加密,其中就包括从没有接触过得aes加密,因此从网上各种查,官方的一种说法: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学 ...
- In-Memory:在内存中创建临时表和表变量
在Disk-Base数据库中,由于临时表和表变量的数据存储在tempdb中,如果系统频繁地创建和更新临时表和表变量,大量的IO操作集中在tempdb中,tempdb很可能成为系统性能的瓶颈.在SQL ...
随机推荐
- 安装Flutter环境
mac 环境安装 系统需求 操作系统: macOS (64-bit) 硬盘: 700 MB 工具: bash, mkdir, rm, git, curl, unzip, which 环境安装 SDK ...
- Nginx初体验(一):nginx介绍
今天我们来介绍一下Nginx. Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件(IMAP/POP3)代理服务器 特点: 反向代理,负载均衡,动静分离 首先我们来介绍一下正向代理服务器 ...
- Java设计模式(4)——创建型模式之单例模式(Singleton)
一.概述 弥补一下之前没有给设计模式下的定义,先介绍一下设计模式(引用自百度百科): 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结. 使用设计 ...
- 《C++ Primer》第II部分:C++标准库
<C++ Primer>第II部分:C++标准库 前言 把<C++ Primer>读薄系列笔记.本篇为第II部分C++标准库,包含全书第8-12章重难点: IO库 顺序容器 范 ...
- Android Parcelable 源码解析
大家都知道,要想在Intent里面传递一些非基本类型的数据,有两种方式,一种实现Parcelable,另一种是实现Serializable接口. 今天先不说Serializable 接口,只说Parc ...
- Qt-第一个QML程序-1-项目工程的建立
这个小程序是我发的第一个完整的QMl程序,这个程序也会持续的更新,一步一步的完善起来,最后会有一个什么样的结果也是不知道,只是把自己目前掌握的QML相关的东西都慢慢的写进来,积累起来 先展示一下运行结 ...
- mapReduce入门教程
什么是MapReduce MapReduce是Google提出的一个软件架构,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归纳)&q ...
- [HNOI2017]影魔
题意: 给定 \(n\) 个数的排列,\(m\) 次询问,每次询问询问一个区间内所有子区间的贡献. 每个区间如果两个端点分别是最大值和次大值,我们就算 \(P1\) 的贡献. 如果两个端点一个是最大值 ...
- ffmpeg接收rtsp流问题
项目使用mingw环境g++5.3,C++调用ffmpeg接收rtsp流,再通过C#显示.结构上是C#调用C++的so文件,读取得到的视频帧(RGB888格式),通过图片控件显示. 一开始是使用ope ...
- Java简单工厂模式
Java简单工厂模式 在阎宏博士的<JAVA与模式>一书中开头是这样描述简单工厂模式的:简单工厂模式是类的创建模式,又叫做静态工厂方法(Static Factory Method)模式.简 ...