Command Handler Command Binding in Xaml : Command « Windows Presentation Foundation « VB.Net






Command Handler Command Binding in Xaml

Command Handler Command Binding in Xaml
    


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="commandWithHandler" Name="root">
  
  <Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Open"
                    Executed="OpenCmdExecuted"
                    CanExecute="OpenCmdCanExecute"/>
  </Window.CommandBindings>


  <Window.InputBindings>
    <KeyBinding Command="ApplicationCommands.Open" Gesture="CTRL+R" />
  </Window.InputBindings>

  <StackPanel>
    <Button Command="ApplicationCommands.Open" Name="MyButton"
            Height="50" Width="200">
      Open (KeyBindings: Ctrl+R, Ctrl+0)
    </Button>
  </StackPanel>
</Window>
//File:Window.xaml.vb

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Input

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub
    Private Sub OpenCmdExecuted(target As Object, e As ExecutedRoutedEventArgs)
      Dim command As [String], targetobj As [String]
      command = DirectCast(e.Command, RoutedCommand).Name
      targetobj = DirectCast(target, FrameworkElement).Name
      MessageBox.Show(command & " : " & targetobj)
    End Sub
    Private Sub OpenCmdCanExecute(sender As Object, e As CanExecuteRoutedEventArgs)
      e.CanExecute = True
    End Sub
  End Class
End Namespace

   
    
    
    
  








Related examples in the same category

1.Invoking Copy command with a ButtonInvoking Copy command with a Button
2.Button CommandTarget BindingButton CommandTarget Binding
3.Creating CommandBinding and attaching an Executed and CanExecute handlerCreating CommandBinding and attaching an Executed and CanExecute handler
4.Property changed callbackProperty changed callback
5.Using CommandBinding and ExecutedRoutedEventHandler to bind Application event to an event handler methodUsing CommandBinding and ExecutedRoutedEventHandler to bind Application event to an event handler method
6.Create CommandBindings in Xaml and bind to ButtonCreate CommandBindings in Xaml and bind to Button
7.Bind TextBox save command to CommandBindingBind TextBox save command to CommandBinding
8.Press F1 to get helpPress F1 to get help
9.Help commandHelp command
10.Command EnablingCommand Enabling