C#/WPF
WPF tutorial - Using WPF commands
광그로
2017. 6. 29. 08:08
1 2 3 4 5 6 7 | <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"></CommandBinding> </Window.CommandBindings> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Button Command="ApplicationCommands.New">New</Button> </StackPanel> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public MainWindow() { InitializeComponent(); } private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("The New command was invoked"); } private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } | cs |
CommandBidings collection을 추가하여 command binding을 창에서 결정할 수 있습니다.
1 2 3 | <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.New" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"></CommandBinding> </Window.CommandBindings> | cs |
code-behind에서 우리는 두가지의 event를 다룰 수 있습니다. WPF는 CanExecute를 응용프로그램이 특정 command가 현재 사용 가능한지에 대한 유휴상태일 때 호출 합니다. 위 예제에서는 CanExecuted 속성을 true로 설정하였습니다.
위 예제에서, Executed 헨들러는 command가 발생했을 때 메세지 박스를보여줍니다. [CTRL] + N 단축키를 통하여 클릭한 것과 같은 동작을 행할 수 있습니다.'
Using the CanExecute method
위 예제에서는 버튼을 항상 사용가능하도록 CanExecute를 true로 설정하였지만, 상황에 따라 활성화 또는 비활성화를 해야합니다.
잘라내기 혹은 복사를 했을 때에만 붙여넣기가 활성화 되는 예제 입니다.
1 2 3 4 5 6 7 8 9 10 11 | <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Cut" Executed="CutCommand_Executed" CanExecute="CutCommand_CanExecute"></CommandBinding> <CommandBinding Command="ApplicationCommands.Paste" Executed="PasteCommand_Executed" CanExecute="PasteCommand_CanExecute"></CommandBinding> </Window.CommandBindings> <DockPanel> <WrapPanel DockPanel.Dock="Top" Margin="3"> <Button Command="ApplicationCommands.Cut" Width="60">_Cut</Button> <Button Command="ApplicationCommands.Paste" Width="60" Margin="3,0">_Paste</Button> </WrapPanel> <TextBox AcceptsReturn="True" Name="txtEditor"></TextBox> </DockPanel> | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public MainWindow() { InitializeComponent(); } private void CutCommand_Executed(object sender, ExecutedRoutedEventArgs e) { txtEditor.Cut(); } private void CutCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = (txtEditor != null) && (txtEditor.SelectionLength > 0); } private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e) { txtEditor.Paste(); } private void PasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = Clipboard.ContainsText(); } | cs |
... _Executed 에서 액션을 결정하고, ... _CanExecute에서 실행될 수 있는 지 없는지의 여부를 결정합니다.
button을 업데이트할 때 이러한 메서드를 항상 호출 할 필요 없이, WPF는 자동으로 수행하여 (유휴상태일 때) 인터페이스가 항상 업데이트됩니다.
Default command behavior and CommandTarget
잘라 내기, 복사, 붙여 넣기, 실행 취소 및 다시 실행 등과 같이 기본적인 command들에 대해서 WPF는 자동으로 처리할 수 있습니다.
1 2 3 4 5 6 7 | <DockPanel> <WrapPanel DockPanel.Dock="Top" Margin="3"> <Button Command="ApplicationCommands.Cut" CommandTarget="{Binding ElementName=txtEditor}" Width="60">_Cut</Button> <Button Command="ApplicationCommands.Paste" CommandTarget="{Binding ElementName=txtEditor}" Width="60" Margin="3,0">_Paste</Button> </WrapPanel> <TextBox AcceptsReturn="True" Name="txtEditor" /> </DockPanel> | cs |
기본적인 command의 처리에 대해서는 이전의 과정들 없이(code-behind에 정의), xaml에서만 처리할 수 있습니다.
단, Button에 CommandTarget을 이용하여 TextBox와 바인딩한 것을 유의하여야 합니다.