Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------MainWindow.xaml------
- <Window x:Class="CSharpWPF.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow"
- Height="350"
- Width="800"
- xmlns:l="clr-namespace:CSharpWPF">
- <Window.InputBindings>
- <KeyBinding Key="S"
- Modifiers="Control"
- Command="{Binding Save}" />
- <KeyBinding Key="A"
- Modifiers="Control"
- Command="{Binding Abort}" />
- </Window.InputBindings>
- <StackPanel>
- <TextBlock Text="{Binding Result,FallbackValue=Status}" />
- <Button Content="Save"
- Command="{Binding Save}" />
- <Button Content="Abort"
- Command="{Binding Abort}" />
- </StackPanel>
- </Window>
- ------MainWindow.xaml.cs------
- using System.Windows;
- namespace CSharpWPF
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- DataContext = new ViewModel();
- }
- }
- }
- ------ViewModel.cs------
- using System;
- using System.ComponentModel;
- using System.Windows.Input;
- namespace CSharpWPF
- {
- class ViewModel : INotifyPropertyChanged
- {
- public ViewModel()
- {
- Save = new SimpleCommand(() => Result = "Save Executed");
- Abort = new SimpleCommand(() => Result = "Abort Executed");
- }
- public ICommand Save { get; private set; }
- public ICommand Abort { get; private set; }
- string _result;
- public string Result
- {
- get
- {
- return _result;
- }
- private set
- {
- _result = value;
- RaisePropertyChanged("Result");
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected void RaisePropertyChanged(string property)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- handler(this, new PropertyChangedEventArgs(property));
- }
- }
- class SimpleCommand : ICommand
- {
- Action action;
- public SimpleCommand(Action action)
- {
- this.action = action;
- }
- public bool CanExecute(object parameter)
- {
- return true;
- }
- public event EventHandler CanExecuteChanged;
- public void Execute(object parameter)
- {
- action();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement