Advertisement
pushpraj

KeyBinding in WPF sample

Sep 24th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. ------MainWindow.xaml------
  2.  
  3. <Window x:Class="CSharpWPF.MainWindow"
  4.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6.         Title="MainWindow"
  7.         Height="350"
  8.         Width="800"
  9.         xmlns:l="clr-namespace:CSharpWPF">
  10.     <Window.InputBindings>
  11.         <KeyBinding Key="S"
  12.                     Modifiers="Control"
  13.                     Command="{Binding Save}" />
  14.         <KeyBinding Key="A"
  15.                     Modifiers="Control"
  16.                     Command="{Binding Abort}" />
  17.     </Window.InputBindings>
  18.     <StackPanel>
  19.     <TextBlock Text="{Binding Result,FallbackValue=Status}" />
  20.         <Button Content="Save"
  21.                 Command="{Binding Save}" />
  22.         <Button Content="Abort"
  23.                 Command="{Binding Abort}" />
  24.     </StackPanel>
  25. </Window>
  26.  
  27.  
  28. ------MainWindow.xaml.cs------
  29.  
  30. using System.Windows;
  31.  
  32. namespace CSharpWPF
  33. {
  34.     /// <summary>
  35.     /// Interaction logic for MainWindow.xaml
  36.     /// </summary>
  37.     public partial class MainWindow : Window
  38.     {
  39.         public MainWindow()
  40.         {
  41.             InitializeComponent();
  42.             DataContext = new ViewModel();
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48.  
  49. ------ViewModel.cs------
  50.  
  51. using System;
  52. using System.ComponentModel;
  53. using System.Windows.Input;
  54.  
  55. namespace CSharpWPF
  56. {
  57.     class ViewModel : INotifyPropertyChanged
  58.     {
  59.         public ViewModel()
  60.         {
  61.             Save = new SimpleCommand(() => Result = "Save Executed");
  62.             Abort = new SimpleCommand(() => Result = "Abort Executed");
  63.         }
  64.         public ICommand Save { get; private set; }
  65.         public ICommand Abort { get; private set; }
  66.  
  67.         string _result;
  68.         public string Result
  69.         {
  70.             get
  71.             {
  72.                 return _result;
  73.             }
  74.             private set
  75.             {
  76.                 _result = value;
  77.                 RaisePropertyChanged("Result");
  78.             }
  79.         }
  80.  
  81.         public event PropertyChangedEventHandler PropertyChanged;
  82.  
  83.         protected void RaisePropertyChanged(string property)
  84.         {
  85.             PropertyChangedEventHandler handler = PropertyChanged;
  86.             if (handler != null)
  87.                 handler(this, new PropertyChangedEventArgs(property));
  88.         }
  89.     }
  90.  
  91.     class SimpleCommand : ICommand
  92.     {
  93.         Action action;
  94.  
  95.         public SimpleCommand(Action action)
  96.         {
  97.             this.action = action;
  98.         }
  99.         public bool CanExecute(object parameter)
  100.         {
  101.             return true;
  102.         }
  103.  
  104.         public event EventHandler CanExecuteChanged;
  105.  
  106.         public void Execute(object parameter)
  107.         {
  108.             action();
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement