public class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged;
/// <summary>
/// 用来帮助命令呼叫者判断命令能不能执行
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
if (this.CanExecuteFunction == null)
return true;
return this.CanExecuteFunction(parameter);
}
/// <summary>
/// 当命令执行的时候做什么
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
if (this.ExecuteAction == null)
return;
//将命令指向委托的方法
this.ExecuteAction(parameter);
}
public Action<object> ExecuteAction { get; set; }
public Func<object, bool> CanExecuteFunction { get; set; }
}
class NameViewMode:NotyfyObject
{
#region 命令
private string name;
public string Name
{
get { return name; }
set {
name = value;
RaisePropertyChanged("Name");
}
}
private string nameE;
public string NameE
{
get { return nameE; }
set {
nameE = value;
RaisePropertyChanged("NameE");
}
}
public RelayCommand AddCommind { get; set; }
private void Add(object parameter)
{
string ss= this.Name ;
this.NameE = this.Name;
}
public NameViewMode()
{
this.AddCommind = new RelayCommand();
this.AddCommind.ExecuteAction = new Action<object>(this.Add);
}
#endregion
}
<Button Content="Button" Command="{Binding AddCommind}" HorizontalAlignment="Left" Margin="192,42,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox HorizontalAlignment="Left" Text="{Binding Name}" Height="23" Margin="313,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Text="{Binding NameE}" Height="23" Margin="313,91,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>