Commanding

Commanding-esimerkki (Commanding.zip)

Routed Commands aktivoivat/disabloivat niihin liittyvät käyttöliittymäkontrollit automaattisesti sen perusteella ilmoittaakö niihin liittyvä käsittelijä komennon olevan käytössä tai pois käytöstä

Komennot ja niiden käyttäminen esitellään hyvin seuraavissa dokumenteissa: Introduction to WPF Commands Commanding Overview

Routed Commandeja käyttävien elementtien ei tarvitse suoraan liittyä vastaaviin käsittelijöihin

Komento:

<Button Name="komentopainike" Command="ApplicationCommands.Save">Save</Button>

<Button Command="Save" CommandTarget="{Binding ElementName=komentopainike}" >Save</Button>

<Window.InputBindings>
  <KeyBinding Key="O"
              Modifiers="Control" 
              Command="ApplicationCommands.Open" />
</Window.InputBindings>

<StackPanel>
  <Menu>
    <MenuItem Command="ApplicationCommands.Paste"
              CommandTarget="{Binding ElementName=mainTextBox}" />
  </Menu>
  <TextBox Name="mainTextBox"/>
</StackPanel>

<!-- Oma komento -->
  <Window.CommandBindings>
    <CommandBinding Command="{x:Static custom:Window1.CustomRoutedCommand}"
                    Executed="ExecutedCustomCommand"
                    CanExecute="CanExecuteCustomCommand" />
  </Window.CommandBindings>

Komennon käsittelijä:

 
<UserControl ...> 
  <UserControl.CommandBindings> 
    <CommandBinding Command="ApplicationCommands.Save" CanExecute="OnCanExecute" Executed="OnExecute"/> 
  </UserControl.CommandBindings>
</UserControl>
KeyGesture OpenKeyGesture = new KeyGesture(
    Key.O,
    ModifierKeys.Control);

KeyBinding OpenCmdKeybinding = new KeyBinding(
    ApplicationCommands.Open,
    OpenKeyGesture);


this.InputBindings.Add(OpenCmdKeybinding);


CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

// yhdistetään komento ikkunaan
this.CommandBindings.Add(OpenCmdBinding);

public static RoutedCommand CustomRoutedCommand = new RoutedCommand();

//CommandBinding customCommandBinding = new CommandBinding(
//    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);

// käytetään lyhyyden vuoksi samoja metodeja mitä open-komennon kanssa
CommandBinding customCommandBinding = new CommandBinding(
    CustomRoutedCommand, OpenCmdExecuted, OpenCmdCanExecute);

this.CommandBindings.Add(customCommandBinding);

Button CustomCommandButton = new Button();

CustomCommandButton.Command = CustomRoutedCommand;

void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    String command, targetobj;
    command = ((RoutedCommand)e.Command).Name;
    targetobj = ((FrameworkElement)target).Name;
    MessageBox.Show("The " + command +  " command has been invoked on target object " + targetobj);
}

void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}

Käyttäjien kommentit

Kommentoi Lisää kommentti
Kurssimateriaalien käyttäminen kaupallisiin tarkoituksiin tai opetusmateriaalina ilman lupaa on ehdottomasti kielletty!
http://appro.mit.jyu.fi/gko/luennot/commands/
© Tommi Lahtonen (tommi.j.lahtonen@jyu.fi) <http://hazor.iki.fi/>
2016-01-13 18:19:13
Informaatioteknologia - Jyväskylän yliopiston informaatioteknologian tiedekunta