|
|
Questions and answers for Concepts
Input and Commands.
|
9. Concepts Input and Commands.
|
| 9.1 What is Commanding in WPF?
|
| 9.2 How do I create a CommandBinding using a RoutedCommand?
|
| 9.3 How can I add a CommandBinding to a window using markup?
|
9.1 What is Commanding in WPF?
|
|
WPF provides a library of common commands, which include
Sample code given below:
|
|
[XAML]
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MenuItemCommandTask">
TextWrapping="Wrap"> The MenuItem will not be enabled until this TextBox gets keyboard focus
|
|
|
9.2 How do I create a CommandBinding using a RoutedCommand?
|
|
Separator controls inside Menu elements appear differently from Separator controls outside a Menu. When you create a Menu with a Separator, the control automatically applies the Style identified by the "SeparatorStyleKey" property. Styles are placed in resource dictionaries and are searched for by their keys. To change the Style of a Separator inside a Menu, you must use the "SeparatorStyleKey" property to create your new Style.
The following example demonstrates this.
|
[XAML]
<Style
x:Key="{x:Static MenuItem.SeparatorStyleKey}"
TargetType="Separator">
<Setter Property="OverridesDefaultStyle"
Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type
Separator}">
<Border Width="30" Height="4"
Margin="4" Background="Red"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
|
|
|
9.3 How can I add a CommandBinding to a window using markup?
|
|
CommandBinding can be added to a window using markup. Note that in XAML, the CommandBindingCollection is not declared in the markup as an element and the collection object is inferred by the type that the property takes and you populate the property element with one or more CommandBinding elements.
|
[XAML]
Executed="OpenCmdExecuted" CanExecute="OpenCmdCanExecute"/>
|
|