Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Created March 7, 2019 11:56
Show Gist options
  • Save wi7a1ian/d65bf5b84040430e4148dbafa96bdab2 to your computer and use it in GitHub Desktop.
Save wi7a1ian/d65bf5b84040430e4148dbafa96bdab2 to your computer and use it in GitHub Desktop.
RoutedUICommand usage example #wpf #csharp
private static RoutedUICommand _pressMeCommand =
new RoutedUICommand("Press Me", "PressMe", typeof(MainWindow));
public static RoutedUICommand PressMeCommand
{
get { return _pressMeCommand; }
}
private void PressMe_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = CowboyCanTalk;
}
private void PressMe_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Howdy howdy I'm a cowboy");
}
<Window.CommandBindings>
<CommandBinding Command="local:MainWindow.PressMeCommand" CanExecute="PressMe_CanExecute" Executed="PressMe_Executed"/>
</Window.CommandBindings>
<Window.ContextMenu>
<ContextMenu>
<MenuItem Command="{x:Static local:MainWindow.PressMeCommand}" />
</ContextMenu>
</Window.ContextMenu>
<Button Command="local:MainWindow.PressMeCommand" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />
private void PressMe_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = CowboyCanTalk;
}
private void PressMe_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Howdy howdy I'm a cowboy");
}
<Window.Resources>
<RoutedUICommand x:Key="PressMeCommand" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource PressMeCommand}" CanExecute="PressMe_CanExecute" Executed="PressMe_Executed"/>
</Window.CommandBindings>
<Button Command="{StaticResource PressMeCommand}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment