Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Dec 14

Written by: Steve Gray
12/14/2010 7:54 AM  RssIcon

This is a code example for a WPF RoutedCommand. It’s a complete example, just copy the code into a Visual Studio WPF Application project and it should run

Here is the XAML for the form

Code Snippet
  1. <Window x:Class="Window1"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.     xmlns:custom="clr-namespace:WpfApplication1"
  5.         
  6.     Title="Window1" Height="300" Width="300">
  7.     <Window.CommandBindings>
  8.         <CommandBinding Command="{x:Static custom:Window1.AddSugar}"
  9.             Executed="ExecutedAddSugar"
  10.             CanExecute="CanExecuteAddSugar" />
  11.         </Window.CommandBindings>
  12.  
  13.  
  14.     <Grid>
  15.         <Menu Margin="0,0,0,234">
  16.             <MenuItem
  17.                 Header="File"
  18.                 Name="mnuFile" />
  19.             <MenuItem
  20.                 Header="Add Sugar"
  21.                 Name="mnuAddSugar"
  22.                 Command="{x:Static custom:Window1.AddSugar}" />
  23.         </Menu>
  24.  
  25.         <Button
  26.             Content="Add Sugar"
  27.             Command="{x:Static custom:Window1.AddSugar}"
  28.             Height="26"
  29.             HorizontalAlignment="Left"
  30.             Margin="24,47,0,0"
  31.             Name="Button1"
  32.             VerticalAlignment="Top"
  33.             Width="130" />
  34.     </Grid>
  35. </Window>

 

Here is the code behind for the form:

 

Code Snippet
  1. Public Class Window1
  2.  
  3.     
  4.     Public Shared Property AddSugar As New RoutedCommand
  5.  
  6.     Private Sub ExecutedAddSugar(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
  7.         MessageBox.Show("Custom Command Executed")
  8.     End Sub
  9.  
  10.     ' CanExecuteRoutedEventHandler that only returns true if
  11.     ' the source is a control.
  12.     Private Sub CanExecuteAddSugar(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
  13.         Dim target As Control = TryCast(e.Source, Control)
  14.  
  15.         If target IsNot Nothing Then
  16.             e.CanExecute = True
  17.         Else
  18.             e.CanExecute = False
  19.         End If
  20.     End Sub
  21.  
  22.  
  23. End Class

Tags:
Categories:
Location: Blogs Parent Separator DEVSHED Blogs Child Separator WPF
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us