wpf中INotifyPropertyChanged的用法

摘要:
=null){returnthis.CanExecuteCommand(参数);}否则{returntrue;}}publicvoidExecute(objectparameter){if(this.ExecuteCommand!

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.Shapes;

namespace WpfApplication1
{
/// <summary>
/// Window9.xaml 的交互逻辑
/// </summary>
public partial class Window9 : Window
{
public Window9()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
class DelegateCommand : ICommand
{
//A method prototype without return value.
public Action<object> ExecuteCommand = null;
//A method prototype return a bool type.
public Func<object, bool> CanExecuteCommand = null;
public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
if (CanExecuteCommand != null)
{
return this.CanExecuteCommand(parameter);
}
else
{
return true;
}
}

public void Execute(object parameter)
{
if (this.ExecuteCommand != null) this.ExecuteCommand(parameter);
}

public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
class Model : NotificationObject
{
private string _wpf = "WPF";

public string WPF
{
get { return _wpf; }
set
{
_wpf = value;
this.RaisePropertyChanged("WPF");
}
}

public void Copy(object obj)
{
this.WPF += " WPF";
}

}
class ViewModel
{
public DelegateCommand CopyCmd { get; set; }
public Model model { get; set; }

public ViewModel()
{
this.model = new Model();
this.CopyCmd = new DelegateCommand();
this.CopyCmd.ExecuteCommand = new Action<object>(this.model.Copy);
}
}

}

________________________________

<Window x:Class="WpfApplication1.Window9"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window9" Width="300">
<Grid>
<StackPanel VerticalAlignment="Top">
<TextBlock Text="{Binding model.WPF}" TextWrapping="WrapWithOverflow"></TextBlock>
<Button Command="{Binding CopyCmd}" Content="Copy" Margin="30,0"/>
</StackPanel>
</Grid>
</Window>

免责声明:文章转载自《wpf中INotifyPropertyChanged的用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇CentOS中ps配合Kill进程的N种方法Sumblime Text 2 常用插件以及安装方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF数据模板样式选择器

在使用数据模板样式选择器时,不能设置ItemContainerStyle的属性值,如果设置了该值,那么数据模板样式选择器会失去作用。 在使用数据模板样式选择器时,首先要创建数据模板样式选择器对象,此对象要重写StyleSelector基类的SelectStyle方法,并在方法中返回相应的样式,CS代码如下: public class ListViewI...

wpf开发常见问题(1)

      经过一段时间wpf的学习和实际开发.现在与大家分享下,在实际中wpf遇到的一些实际问题.silverlight 2.0正式版已经出来.sliverlight的功能应该与wpf大步分类似。其中的经验照样可以套用到sliverlight上.现在开始. 一.与模板相关问题 1.如何取得模板中的元素? 直切重点 (1)第一步确定控件相关ContentP...

WPF 显示3D密集场景,堆场管理系统

又好久好久没写博客了,这次接着上文https://www.cnblogs.com/CSSZBB/p/12785380.html,上文用WPF 的绘图功能,制作了一个伪3D的2.5D控件ThreeDBox ,那么这玩意我是用来干啥那?没事做做着玩么? 简单来说,这个东西用来显示密集型的操作画面,比如显示一个堆场里集装箱的摆放情况,是不是稍微有点像了那?当然为...

Wpf(Storyboard)动画简单实例

动画的三种变换方式 RotateTransform:旋转变换变化值:CenterX围绕转的圆心横坐标 CenterY纵坐标 Angle旋转角度(角度正负表示方向) ScaleTransform:缩放变换变化值:ScaleX横向放大倍数ScaleY纵向(负值时翻转) TranslateTransform:平移变换变化值:X横坐标Y纵坐标 其中 <...

贫民窟里的WPF系列讲座(一)

最近讲了一套完整的WPF课程,感觉教学效果还很不错。我准备给MSDN录15期左右的事情,这几篇文章是配合视频一起来看的。我相信大家都是很喜欢研究新技术的,但是研究新技术的时候会遇到很多困难,譬如书籍的选择,工具的选择,环境的配置等等很多很多问题。 这里我结合自己学习的历程,分享一下。这里我说一下我当时学习的硬件条件,由于我的笔记本是IBM的T40,CPU是...

C/S C# WPF锐浪报表教程

前言:锐浪报表是一种中国式报表的报表开发工具。博主使用锐浪报表有一段时间了,积累了一些经验希望能帮助你快速掌握并使用 第一章:集成项目 首先我们先去锐浪报表官网下载并安装锐浪报表。 创建WPF应用程序。(C/S端使用锐浪报表基本都一样) 添加锐浪报表的引用,在资源管理器目录中找到引用并右键,点击添加引用。 在引用管理器左侧目录中展开COM并找到Gri...