我創建了一個按鈕,其commandparameter被設置並且使用實現ICommand接口的類來執行命令。但我的按鈕被禁用。這是爲什麼?我從這裏驗證碼:ICommand is like a chocolate cake即使在提供WPF中的CommandParameter後,按鈕仍未啓用
<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>
和我的課是
class HelloWorldCommand:ICommand
{
public bool CanExecute(object parameter)
{
return parameter != null;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show(parameter.ToString());
}
}
+1我剛剛開始鍵入複雜的解釋,但你更快:) – JleruOHeP
+1但有沒有VB版本? –
@NikhilAgrawal,看看這篇文章:http://waxtadpole.wordpress.com/2009/10/21/wpf-mvvm-in-vb-net-delegating-event-subscriptions-for-routedcommand-or-any-icommand -in-the-viewmodel/ – Dennis