本文共 2771 字,大约阅读时间需要 9 分钟。
WPF可以直接在控件上捆绑一些常用的命令如复制、粘贴、剪贴、撤销、恢复,通过指定目标控件直接生效。
相当方便,不用使用命令控制。
1、设计器直接控件跟ApplicationCommands命令捆绑:
2、捆绑命令响应的控件
在【应用数据绑定】中找到要响应命令的控件
3、控件设置内置样式(静态资源)
下面为一些较好的BUTTON等样式的控件模板,将其放到XAML文件中即成为StaticResources
- UserControl.Resources>
- !-- Flat Button -->
- ControlTemplate x:Key="FlatButtonControlTemplate" TargetType="{x:Type Button}">
- Border x:Name="OuterBorder" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
- Border x:Name="InnerBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
- ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" Margin="{TemplateBinding Padding}" />
- /Border>
- /Border>
- ControlTemplate.Triggers>
- Trigger Property="IsMouseOver" Value="True">
- Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF7CA0CC" />
- Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FFE4EFFD" />
- Setter TargetName="InnerBorder" Property="Background" Value="#FFDAE7F5" />
- /Trigger>
- Trigger Property="IsPressed" Value="True">
- Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF2E4E76" />
- Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FF116EE4" />
- Setter TargetName="InnerBorder" Property="Background" Value="#FF3272B8" />
- /Trigger>
- /ControlTemplate.Triggers>
- /ControlTemplate>
-
- !-- Flat Toggle Button -->
- ControlTemplate x:Key="FlatToggleButtonControlTemplate" TargetType="{x:Type ToggleButton}">
- Border x:Name="OuterBorder" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
- Border x:Name="InnerBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
- ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" Margin="{TemplateBinding Padding}" />
- /Border>
- /Border>
- ControlTemplate.Triggers>
- Trigger Property="IsMouseOver" Value="True">
- Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF7CA0CC" />
- Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FFE4EFFD" />
- Setter TargetName="InnerBorder" Property="Background" Value="#FFDAE7F5" />
- /Trigger>
- Trigger Property="IsPressed" Value="True">
- Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF2E4E76" />
- Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FF116EE4" />
- Setter TargetName="InnerBorder" Property="Background" Value="#FF3272B8" />
- /Trigger>
- Trigger Property="IsChecked" Value="True">
- Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FFFFC00A" />
- Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FFFFDE7F" />
- Setter TargetName="InnerBorder" Property="Background" Value="#FFFFD458" />
- /Trigger>
- /ControlTemplate.Triggers>
- /ControlTemplate>
- /UserControl.Resources>
控件调用上述模板来美化外观:
案例效果:
参考工程:
转载地址:http://sgkix.baihongyu.com/