小智 119
为什么每个人都这么复杂!
<TextBox x:Name="TB"/>
<Button IsEnabled="{Binding ElementName=TB,Path=Text.Length}">Test</Button>
Run Code Online (Sandbox Code Playgroud)
没有别的......
使用简单命令
<TextBox Text={Binding Path=TitleText}/>
<Button Command="{Binding Path=ClearTextCommand}" Content="Clear Text"/>
Run Code Online (Sandbox Code Playgroud)
这是视图模型中的示例代码
public class MyViewModel : INotifyPropertyChanged
{
public ICommand ClearTextCommand { get; private set; }
private string _titleText;
public string TitleText
{
get { return _titleText; }
set
{
if (value == _titleText)
return;
_titleText = value;
this.OnPropertyChanged("TitleText");
}
}
public MyViewModel()
{
ClearTextCommand = new SimpleCommand
{
ExecuteDelegate = x => TitleText="",
CanExecuteDelegate = x => TitleText.Length > 0
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅 Marlon Grechs SimpleCommand
另请查看http://blogs.msdn.com/llobo/archive/2009/05/01/download-mv-vm-project-template-toolkit.aspx中的 MVVM 项目模板/工具包。它使用 DelegateCommand 进行命令,对于任何项目来说它都应该是一个很好的起始模板。