我创建使用实现ICommand接口的类的commandparameter设置和命令按钮。但我的按钮将被禁用。这是为什么?我得到这个code从这里开始: ICommand的是像一个巧克力蛋糕
<窗口x:类=ICommand_Implementation_CSharp.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
XMLNS:地方=CLR的命名空间:ICommand_Implementation_CSharp
标题=主窗口高度=350宽度=525>
<电网>
<电网>
< Grid.Resources>
<地方:HelloWorldCommand X:关键=HWC/>
< /Grid.Resources>
<按钮命令={的StaticResource HWC}CommandParameter =你好
身高=23的HorizontalAlignment =左保证金=212,138,0,0
NAME =Button1的VerticalAlignment =热门WIDTH =75>按钮和LT; /按钮>
< /网格>
< /网格>
和我的课是
类HelloWorldCommand:ICommand的
{
公共BOOL CanExecute(对象参数)
{
返回参数!= NULL;
}
公共事件的EventHandler CanExecuteChanged;
公共无效执行(对象参数)
{
的MessageBox.show(parameter.ToString());
}
}
解决方案
那么,这是非常,非常简单的实现的ICommand
的。
由于@JleruOHeP说,部分问题是可以通过交换命令
和 CommandParameter
的制定者解决。但是,这是丑陋的方式,因为你每次都记住序列。
更正确的做法是告诉命令管理
命令来重新查询状态:
公共类HelloWorldCommand:ICommand的
{
公共BOOL CanExecute(对象参数)
{
返回参数!= NULL;
}
公共事件的EventHandler CanExecuteChanged
{
添加{CommandManager.RequerySuggested + =价值; }
除去{CommandManager.RequerySuggested - =价值; }
}
公共无效执行(对象参数)
{
的MessageBox.show(parameter.ToString());
}
}
现在制定者的顺序是无动于衷。
要了解如何命令管理
的作品,你可以阅读这个的距离约什 - 史密斯不错的文章。
I created a button whose commandparameter is set and command using a class that implements ICommand interface. But my button is disabled. Why is that? I got this code from here: ICommand is like a chocolate cake
<Window x:Class="ICommand_Implementation_CSharp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ICommand_Implementation_CSharp"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid>
<Grid.Resources>
<local:HelloWorldCommand x:Key="hwc" />
</Grid.Resources>
<Button Command="{StaticResource hwc}" CommandParameter="Hello"
Height="23" HorizontalAlignment="Left" Margin="212,138,0,0"
Name="Button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>
</Grid>
and my class is
class HelloWorldCommand:ICommand
{
public bool CanExecute(object parameter)
{
return parameter != null;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show(parameter.ToString());
}
}
解决方案
Well, this is very-very simple implementation of ICommand
.
As @JleruOHeP says, partially problem can be solved by swapping setters of Command
and CommandParameter
. But this is ugly way, because you have to remember the sequence every time.
More correct way is to tell CommandManager
to re-query states of command:
public class HelloWorldCommand : ICommand
{
public bool CanExecute(object parameter)
{
return parameter != null;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
MessageBox.Show(parameter.ToString());
}
}
Now the sequence of setters is indifferent.
To understand, how CommandManager
works, you can read this nice article from Josh Smith.