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 ...
随机推荐
- 洛谷P4526 【模板】自适应辛普森法2(simpson积分)
题目描述 计算积分 保留至小数点后5位.若积分发散,请输出"orz". 输入输出格式 输入格式: 一行,包含一个实数,为a的值 输出格式: 一行,积分值或orz 输入输出样例 输入 ...
- 推荐 的FPGA设计经验(3) 物理实现和时间闭环优化
Optimizing Physical Implementation and Timing Closure Planning Physical Implementation When planning ...
- Caliburn.Micro 项目文档(翻译):Screens, Conductors and Composition
原文地址(项目说明文档):[Documentation Screens, Conductors and Composition]http://caliburnmicro.codeplex.com/w ...
- 北京Uber优步司机奖励政策(4月5日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 武汉Uber优步司机奖励政策(12月21日-12.27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- CF 600 E. Lomsat gelral
E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号 ...
- vue.js中引入其他文件export的方法:
import {GetPosition} from '../../lib/utils' //找到 该方法的文件路径,然后 用{}拿到 该方法 var position =GetPosition(); ...
- [JSON].toXMLString()
语法:[JSON].toXMLString() 返回:[String] 说明:将[JSON]实例转换成XML格式结果. 示例: <% jsonString = "{div: 'hell ...
- Eclipse上安装Activiti插件
今天我们来讲下如何在Eclipse上安装Activiti插件,以后我们要用这个插件来画流程设计图: 这个插件名字是:Activiti BPMN 2.0 designer 具体使用,可以参考官方用户指南 ...
- 《Effective C++》读书笔记 被你忽略的关于构造析构赋值
如果程序员没有定义,那么编译器会默认隐式为你创建一个copy构造函数,一个copy赋值操作符,一个析构函数.另外如果你没有声明任何构造函数,编译器会为你声明一个default构造函数. 但是只有当这些 ...