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 ...
随机推荐
- 由OpenResty粘合的企业Web架构
前言: 2012年2月章亦春(agentzh)在Tech-Club的一次线下聚会上以<由Lua 粘合的Nginx生态环境>为主题做了演讲,分析了企业Web架构的趋势,即一个看起来完整 ...
- SpringBoot-07:SpringBoot整合PageHelper做多条件分页查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述如何在SpringBoot中整合PageHelper,如何实现带多个条件,以及PageInfo中的 ...
- 海思NB-IOT死机问题解决记录
1. 首先抓下LOG的信息,这个应该是MCU到NB的数据 2. 看下NB到MCU的数据 3. 总结起来,只看到了NB模块的启动,似乎AT指令,肯本没收到.测试中确实出现了复位,但是不清楚是发送的AT指 ...
- 使用Unity创建依赖注入
这篇文章翻译自<Dependency Injection With Unity>第三章.文中提到的类似"前几节"的内容您不必在意,相信您可以看懂的. P.S:如 ...
- Python Road
引子 雁离群兮不知所归,路遥远兮吾将何往 Python Road[第一篇]:Python简介 Python Road[第二篇]:Python基本数据类型 Python Road[第三篇]:Pyth ...
- Selenium(Python) ddt数据驱动
首先, 添加ddt模块: import unittestfrom time import sleep from ddt import ddt, data, unpack# 导入ddt模块from se ...
- linux学习总结---mysql总结②
函数: 字符串 日期时间 数学 子查询:嵌套查询 1. 做子查询: select sclass from studb where sname='..' 2.select * from studb wh ...
- hexo部署失败如何解决
- Spark mlib的本地向量
Spark mlib的本地向量有两种: DenseVctor :稠密向量 其创建方式 Vector.dense(数据) SparseVector :稀疏向量 其创建方式有两种: 方法一:Vector. ...
- java核心技术 笔记
一 . 总览 1. 类加载机制:jdk内嵌的class_loader有哪些,类加载过程.--后面需要补充 2. 垃圾收集基本原理,常见的垃圾收集器,各自适用的场景.--后面需要补充 3. 运行时动态编 ...