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>
The interactive logic of 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" height= "width=" >
<Grid>
<stackpanel verticalalignment= "Top" >
<textblock text= "{Binding model. WPF} "height=" 208 "textwrapping=" Wrapwithoverflow "></TextBlock>
<button command= "{Binding copycmd}" height= "" width= "232" content= "Copy" margin= "30,0"/>
</StackPanel>
</Grid>
</Window>
Usage of inotifypropertychanged in WPF