WPF Command with Click Event Handler
When I use the Command
in a Button
control the event handler which joined with Click
event will 开发者_StackOverflow中文版never raised,
How can I use the Command
and handle the Click
event handler?
You could attach the ICommand to another property and execute it from the Click handler.
<Button x:Name="MyButton" Tag="{x:Static ApplicationCommands.Stop}" Click="MyButton_Click" />
and in the handler:
private void MyButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
var command = button.Tag as ICommand;
if (command != null)
command.Execute(button.CommandParameter);
}
}
You'd also need to do some extra work if you wanted to keep the Command disabling behavior.
You need the EventToCommand behaviour in MVVMLight
A command is a kind of week event. The good thinks about commands is that you do not need to use events. Actually you do not need to use code behind at all and events if you use a Viewmodel pattern. See relay commands: http://blog.galasoft.ch/archive/2009/09/26/using-relaycommands-in-silverlight-and-wpf.aspx
精彩评论