Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10425262
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2016-03-25 16:55:37

WPF可以直接在控件上捆绑一些常用的命令如复制、粘贴、剪贴、撤销、恢复,通过指定目标控件直接生效。

相当方便,不用使用命令控制。

1、设计器直接控件跟ApplicationCommands命令捆绑:

image

2、捆绑命令响应的控件

image

在【应用数据绑定】中找到要响应命令的控件

image

3、控件设置内置样式(静态资源)

下面为一些较好的BUTTON等样式的控件模板,将其放到XAML文件中即成为StaticResources


  1. <UserControl.Resources>
  2.         <!-- Flat Button -->
  3.         <ControlTemplate x:Key="FlatButtonControlTemplate" TargetType="{x:Type Button}">
  4.             <Border x:Name="OuterBorder" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
  5.                 <Border x:Name="InnerBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
  6.                     <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" Margin="{TemplateBinding Padding}" />
  7.                 </Border>
  8.             </Border>
  9.             <ControlTemplate.Triggers>
  10.                 <Trigger Property="IsMouseOver" Value="True">
  11.                     <Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF7CA0CC" />
  12.                     <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FFE4EFFD" />
  13.                     <Setter TargetName="InnerBorder" Property="Background" Value="#FFDAE7F5" />
  14.                 </Trigger>
  15.                 <Trigger Property="IsPressed" Value="True">
  16.                     <Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF2E4E76" />
  17.                     <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FF116EE4" />
  18.                     <Setter TargetName="InnerBorder" Property="Background" Value="#FF3272B8" />
  19.                 </Trigger>
  20.             </ControlTemplate.Triggers>
  21.         </ControlTemplate>

  22.         <!-- Flat Toggle Button -->
  23.         <ControlTemplate x:Key="FlatToggleButtonControlTemplate" TargetType="{x:Type ToggleButton}">
  24.             <Border x:Name="OuterBorder" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
  25.                 <Border x:Name="InnerBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1" CornerRadius="2">
  26.                     <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" Margin="{TemplateBinding Padding}" />
  27.                 </Border>
  28.             </Border>
  29.             <ControlTemplate.Triggers>
  30.                 <Trigger Property="IsMouseOver" Value="True">
  31.                     <Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF7CA0CC" />
  32.                     <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FFE4EFFD" />
  33.                     <Setter TargetName="InnerBorder" Property="Background" Value="#FFDAE7F5" />
  34.                 </Trigger>
  35.                 <Trigger Property="IsPressed" Value="True">
  36.                     <Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FF2E4E76" />
  37.                     <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FF116EE4" />
  38.                     <Setter TargetName="InnerBorder" Property="Background" Value="#FF3272B8" />
  39.                 </Trigger>
  40.                 <Trigger Property="IsChecked" Value="True">
  41.                     <Setter TargetName="OuterBorder" Property="BorderBrush" Value="#FFFFC00A" />
  42.                     <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FFFFDE7F" />
  43.                     <Setter TargetName="InnerBorder" Property="Background" Value="#FFFFD458" />
  44.                 </Trigger>
  45.             </ControlTemplate.Triggers>
  46.         </ControlTemplate>
  47.     </UserControl.Resources>


image

控件调用上述模板来美化外观:

image

案例效果:

image

参考工程:
SourceCode.zip

阅读(1493) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~