这是一个简单的屏幕,textblock
最初的屏幕为“”,button
称为“设置文本”,用于将文本设置为textblock
,another button
称为“清除文本”,其始终清除textblock
。这就是XAML的样子。
<StackPanel>
<TextBlock Text="{Binding DisplayText, Mode=TwoWay}"></TextBlock>
<Button Content="Set Text" Command="{Binding SetTextCommand}"></Button>
<Button Content="Clear Text" Command="{Binding CancelCommand}"
IsEnabled="{Binding CanCancel, Mode=TwoWay}"/>
</StackPanel>
这是我的ViewModel代码。
public class Page1VM : ViewModelBase
{
public RelayCommand SetTextCommand { get; private set; }
public RelayCommand CancelCommand { get; private set; }
public Page1VM()
{
SetTextCommand = new RelayCommand(HandleSetText, CanExecute);
CancelCommand = new RelayCommand(HandleClearButtonClick, CanExecuteCancel);
}
private void HandleSetText(string number)
{
DisplayText = number;
}
private string _displayText="";
public string DisplayText
{
get { return _displayText; }
set
{
_displayText = value;
RaisePropertyChanged("DisplayText");
RaisePropertyChanged("CanCancel");
}
}
private bool _canCancel;
public bool CanCancel
{
get
{
if (DisplayText == "")
{
return false;
}
else
{
return true;
}
}
set
{
_canCancel = value;
RaisePropertyChanged("CanCancel");
}
}
private bool CanExecute()
{
return true;
}
private bool CanExecuteCancel()
{
if (DisplayText == "")
{
return false;
}
else
{
return true;
}
}
private void HandleClearButtonClick()
{
DisplayText = "";
}
private void HandleSetText()
{
DisplayText = "Hello";
}
}
问题:加载页面后,“清除文本”按钮被禁用,这是预期的,并且可以按预期工作。
当我单击“设置文本”时,我通过将文本值设置为named属性DisplayText
并调用RaisePropertyChanged("CanCancel");
而将文本设置为文本块,但是即使在未启用“清除文本”按钮之后,我也将其设置为文本块。背后的原因可能是什么?我的文本块显示了文本值,但“明文”按钮仍未启用。
据我所知,您的示例中有些混乱:您基本上不使用“ RelayCommand”的内置“ CanExecute”机制,而是在自己定义CanExecute
“ RealyCommand ”方法的同时重新构建它'。“ CanExecute”的想法是自动取消无法执行命令的控件,因此您无需手动执行。在“ CanExecute”方法中返回“ true”实际上没有任何意义,因为您不一定需要在RelayCommand中有一个CanExecute委托(... = new RelayCommand(ExecuteCommand);
很好)。您的方案不起作用,因为您没有在“ CancelCommand”上调用“ RaisCanExecuteChanged()”。
尝试以下实现,我已经删除了多余的部分,并插入了缺少的“ RaiseCanExecuteChanged()”。请参阅注释以获取解释:
<StackPanel>
<TextBlock Text="{Binding DisplayText, Mode=TwoWay}"></TextBlock>
<Button Content="Set Text" Command="{Binding SetTextCommand}"></Button>
<Button Content="Clear Text" Command="{Binding CancelCommand}" />
</StackPanel>
并使用此简化的ViewModel:
public class Page1VM : ViewModelBase
{
public RelayCommand SetTextCommand { get; private set; }
public RelayCommand CancelCommand { get; private set; }
public Page1VM()
{
SetTextCommand = new RelayCommand(ExecuteSetText);
CancelCommand = new RelayCommand(ExecuteCancel, CanExecuteCancel);
}
private string _displayText="";
public string DisplayText
{
get { return _displayText; }
set
{
_displayText = value;
RaisePropertyChanged("DisplayText");
RaisePropertyChanged("CanCancel");
// Raise the CanExecuteChanged event of CancelCommand
// This makes the UI reevaluate the CanExecuteCancel
// Set a breakpoint in CanExecuteCancel method to make
// sure it is hit when changing the text
CancelCommand.RaiseCanExecuteChanged();
}
}
private bool CanExecuteCancel()
{
// You can simplify the statement like this:
return DisplayText != "";
}
private void ExecuteCancel()
{
DisplayText = "";
}
private void ExecuteSetText()
{
DisplayText = "Hello";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句