wpf 满足条件时,CanExecute() 不启用按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25828572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CanExecute() not enabling button when condition is met
提问by user3761858
I have a very simple application with a TextBoxand a Button. When the text entered into the TextBoxexceeds 5 characters in length, the button will be enabled. Here is the code for my ViewModel:
我有一个非常简单的应用程序,带有 aTextBox和 a Button。当输入的文本TextBox长度超过 5 个字符时,该按钮将被启用。这是我的 ViewModel 的代码:
private string _text { get; set; }
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
}
}
private ICommand _buttonCommand;
public ICommand ButtonCommand
{
get
{
if (_buttonCommand == null)
{
_buttonCommand = new RelayCommand(
param => this.ButtonCommandExecute(),
param => this.ButtonCommandCanExecute()
);
}
return _buttonCommand;
}
}
private bool ButtonCommandCanExecute()
{
if (this.Text.Length < 5)
{
return false;
}
else
{
return true;
}
}
private void ButtonCommandExecute()
{
this.Text = "Text changed";
}
public MainWindowViewModel()
{
//
}
The TextBoxand Buttonare bound using this XAML:
该TextBox和Button使用此XAML约束:
<Button Content="Button" HorizontalAlignment="Left"
Margin="185,132,0,0" VerticalAlignment="Top" Width="120"
Command="{Binding Path=ButtonCommand}" />
<TextBox HorizontalAlignment="Left" Height="23"
Margin="185,109,0,0" TextWrapping="Wrap"
Text="{Binding Path=Text, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
The DataContextappears to be set correctly, but here it is just because I am a WPF beginner:
在DataContext看似正确设置,但在这里它仅仅是因为我是一个WPF初学者:
private MainWindowViewModel view_model;
public MainWindow()
{
InitializeComponent();
view_model = new MainWindowViewModel();
this.DataContext = view_model;
}
When I type into the TextBox, the Buttonnever enables.
当我输入 时TextBox,Button永远不会启用。
采纳答案by loop
Actually you have to make Bool Property to bind to the IsEnabled property of the Button Control. And set this property to true when your text in Textbox is more than five character - you have to do this in Setter of Text property Because this is what being called when you type in your TextBox.
实际上,您必须使 Bool 属性绑定到按钮控件的 IsEnabled 属性。当您在 Textbox 中的文本超过五个字符时,将此属性设置为 true - 您必须在 Text 属性的 Setter 中执行此操作,因为这是您在 TextBox 中键入时调用的内容。
Basic About Commands:- These are basically to Report the e.g Clicks events to the C# code say your Viewmodel/Page.cs . So that you can perform some Tasks. It is not related to anything about Enabling and disabling of button.
关于命令的基本信息:- 这些基本上是将例如 Clicks 事件报告给 C# 代码,比如您的 Viewmodel/Page.cs。这样你就可以执行一些任务。它与有关启用和禁用按钮的任何内容无关。
Follow the Code :-
遵循守则:-
private string _text { get; set; }
public string Text
{
get { return _text; }
set
{
_text = value;
if(_text.Length > 5)
// Enable button here
// and command does not enable Buttons they are basically report the clicks events.
IsButtonEnabled = true;
OnPropertyChanged("Text");
}
}
For Enabling Button Create Bool type property Called IsButtonEnabled and bind this property to your Button in Xaml.
对于启用按钮创建布尔类型属性称为 IsButtonEnabled 并将此属性绑定到 Xaml 中的按钮。
private bool _IsButtonEnabled { get; set; }
public bool IsButtonEnabled
{
get { return _IsButtonEnabled ; }
set
{
_IsButtonEnabled = value;
OnPropertyChanged("IsButtonEnabled");
}
}
In Xaml :-
在 Xaml 中:-
<Button Content="Button" HorizontalAlignment="Left"
IsEnabled="{Binding IsButtonEnabled}"
Margin="185,132,0,0" VerticalAlignment="Top" Width="120"
Command="{Binding Path=ButtonCommand}" />
回答by Sergey Brunov
Some implementations of ICommandinterface have special method to notify whether "CanExecute" has changed. RelayCommandclass (MVVM Light) has such method.
ICommand接口的一些实现有特殊的方法来通知“CanExecute”是否已经改变。RelayCommand类(MVVM Light)有这样的方法。
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
// There is a special RelayCommand method to notify "CanExecute" changed.
// After this call, the "CanExecute" state is "re-evaluated" automatically by binding using CanExecute Func passed into RelayCommand constructor.
_buttonCommand.RaiseCanExecuteChanged();
}
}
private RelayCommand _buttonCommand;
public ICommand ButtonCommand
{
get
{
if (_buttonCommand == null)
{
_buttonCommand = new RelayCommand(
param => this.ButtonCommandExecute(),
param => this.ButtonCommandCanExecute()
);
}
return _buttonCommand;
}
}
This question can be useful: What is CanExecuteChanged for?
这个问题很有用:什么是 CanExecuteChanged?
回答by ericpap
Try this Little modification of your code and tell me if it Works:
尝试对您的代码稍作修改并告诉我它是否有效:
private string _text { get; set; }
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
ButtonCommandCanExecute();
}
}
private ICommand _buttonCommand;
public ICommand ButtonCommand
{
get
{
if (_buttonCommand == null)
{
_buttonCommand = new RelayCommand(
param => this.ButtonCommandExecute(),
param => this.ButtonCommandCanExecute()
);
}
return _buttonCommand;
}
}
private bool ButtonCommandCanExecute()
{
if (this.Text.Length < 5)
{
return false;
}
else
{
return true;
}
}
private void ButtonCommandExecute()
{
this.Text = "Text changed";
}
public MainWindowViewModel()
{
//
}

