在WPF中,当用户在TextBox
中键入内容时启用Button
的最简单方法是什么
发布于 2009-05-04 19:01:41
使用简单命令
<TextBox Text={Binding Path=TitleText}/>
<Button Command="{Binding Path=ClearTextCommand}" Content="Clear Text"/>
以下是View Model中的示例代码
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));
}
}
有关更多信息,请参阅Marlon Grechs SimpleCommand
也可以从http://blogs.msdn.com/llobo/archive/2009/05/01/download-m-v-vm-project-template-toolkit.aspx查看MVVM Project Template/Toolkit。它使用DelegateCommand进行命令,它应该是任何项目的一个很好的启动模板。
发布于 2010-01-18 20:31:58
为什么每个人都把事情搞得这么复杂!
<TextBox x:Name="TB"/>
<Button IsEnabled="{Binding ElementName=TB,Path=Text.Length}">Test</Button>
不需要别的了......
发布于 2009-12-03 18:11:13
使用触发器!
<TextBox x:Name="txt_Titel />
<Button Content="Transfer" d:IsLocked="True">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=txt_Titel, Path=Text}" Value="">
<Setter Property="Button.IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
https://stackoverflow.com/questions/821121
复制相似问题