第17章 控件模版(3)——属性触发器
来源:互联网 发布:阳春巿网络问政 编辑:程序博客网 时间:2024/05/17 03:41
一、自定义按钮模版xaml代码
首先为模版中的元素设置Name属性,方便后面触发器设置。
属性触发器当关注的属性符合预设值时,自动应用对应的属性设置。当属性变化后会自动恢复到设置前的状态。
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ButtonTemplate" xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="ButtonTemplate.MainWindow" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}"> <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="Red" TextBlock.Foreground="White" Name="Border"> <Grid> <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2" SnapsToDevicePixels="True"></Rectangle> <ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"></ContentPresenter> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Border" Property="Background" Value="DarkRed" /> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="Border" Property="Background" Value="IndianRed" /> <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" /> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter TargetName="FocusCue" Property="Visibility" Value="Visible" /> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="Border" Property="TextBlock.Foreground" Value="Gray" /> <Setter TargetName="Border" Property="Background" Value="MistyRose" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Window.Resources> <StackPanel Margin="10"> <Button Margin="10" Padding="5" Template="{StaticResource ButtonTemplate}" Name="cmdOne">A Simple Button with a Custom Template</Button> <Button Margin="10" Padding="5" Template="{StaticResource ButtonTemplate}" Name="cmdTwo">Another Button with a Custom Template</Button> <Button Margin="10" Padding="5" Template="{StaticResource ButtonTemplate}" Name="cmdThree">A Third Button</Button> <Button Margin="10" Padding="5" Template="{StaticResource ButtonTemplate}" Name="cmdFour" IsEnabled="False">A Disabled Button</Button> </StackPanel></Window>注意:
①IsEnabled触发器要放在最后定义,这样可确保该规则优先于其它冲突的触发器设置。
②通过IsKeyboardFocused触发器实现虚线边框Rectangle的显示
二、效果演示
1 0
- 第17章 控件模版(3)——属性触发器
- 第17章 控件模版(4)——事件触发器
- 第17章 控件模版(2)——模版绑定
- 第17章 控件模版(1)——模版的创建与使用
- 第17章 控件模版(5)——组织模版资源概述
- 第17章 控件模版(6)——通过样式应用模版
- 第17章 控件模版(7)——通过样式自动应用模版
- 第17章 控件模版(8)——动态换肤
- 第13章 触发器
- 模版——函数模版、类模版
- 第4章 依赖属性(4)——属性验证
- Flask Web开发 第一部分 第3章 模版
- OpenCV2马拉松第13圈——模版匹配
- 第4章 依赖属性(3)——自定义附加属性
- 【C++ Primer】第16章 模版与泛型模版
- Attributions——属性控件
- 第15章 动画基础(2)——故事板、触发器
- 【木头Cocos2d-x 035】我是定时器(第04章)—停止update和触发器
- ac自动机背诵用模板
- 解决8080端口占用问题
- 代码块
- iOS小问题(二)
- firewalk ----- 主动网络探测工具
- 第17章 控件模版(3)——属性触发器
- 运算符和语句
- Android中的ViewDragHelper
- 《设计模式之禅》读书笔记(五)
- 未来计算机将何去何从?
- 1051. 复数乘法 (15)
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- 【视频】聊聊投篮
- Nginx与tomcat组合的简单使用