Created
March 7, 2019 11:56
-
-
Save wi7a1ian/d65bf5b84040430e4148dbafa96bdab2 to your computer and use it in GitHub Desktop.
RoutedUICommand usage example #wpf #csharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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}" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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