【发布时间】:2014-11-05 13:05:19
【问题描述】:
我正在 WPF 中寻找一种解决方案,以根据文本框的内容更改按钮的 IsEnabled 属性。 TextBox 包含一个数值。如果该值大于某个值,则按钮的 IsEnabled 属性应设置为 true,只要低于此值,该属性就应设置为 false。 我一直在环顾四周,但找不到合适的解决方案。我在CodeProject 上找到的几乎就是我要找的。但问题是这种方法只是检查文本框中是否有任何内容。但我需要检查/比较数字内容。
我更愿意在 XAML 中找到一种方法。或者,我也可以在我的 ViewModel 中实现它。但我不知道该怎么做!我正在考虑通过文本框中显示的属性中的 INotifyChanged 事件通知按钮。但我不知道怎么做。
遵循一些代码。但是,对不起,文本框和按钮旁边什么都没有,因为我找不到解决方法。
<TextBox Name ="tbCounter" Text ="{Binding CalcViewModel.Counter, Mode=OneWay}" Background="LightGray" BorderBrush="Black" BorderThickness="1"
Height="25" Width="50"
commonWPF:CTextBoxMaskBehavior.Mask="Integer"
commonWPF:CTextBoxMaskBehavior.MinimumValue="0"
commonWPF:CTextBoxMaskBehavior.MaximumValue="1000"
IsReadOnly="True"/>
<Button Name="btnResetCount" Focusable="True" Content="Reset" Command="{Binding Path=CalcViewModel.ResetCounter}" Style="{StaticResource myBtnStyle}"
Width="100" Height="25">
是否有一种通用方法可以根据 XAML 或 ViewModel 中的属性/值设置控件的 IsEnabled 属性?
编辑这是我的 ViewModel,我只提取了相关的成员和属性,否则帖子会太长:
class CalcViewModel:INotifyPropertyChanged
{
private CCalc _calc;
public int Counter
{
get
{ return _calc.Counter; }
set{ _calc.Counter = value;}
}
public event PropertyChangedEventHandler PropertyChanged;
void ResetCounterExecute()
{ _calc.Counter = 0; }
bool CanResetCounterExecute()
{
if (_calc.Counter > 0)
{ return true; }
else
{ return false; }
}
public ICommand ResetCounter
{ get { return new RelayCommand(ResetCounterExecute, CanResetCounterExecute); } }
public CCalcViewModel()
{
this._calc = new CCalcViewModel();
this._calc.PropertyChanged += new PropertyChangedEventHandler(OnCalcPropertyChanged);
}
private void OnCalcPropertyChanged(object sender, PropertyChangedEventArgs e)
{
this.RaisePropertyChanged(e.PropertyName);
}
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】: