1. 在style中使用trigger无效的场景

  原因是直接在对象上设置值将导致style中的值无效,去掉TextBlock对象的Foreground后,Trigger将正常工作

        <TextBlock Text="AAA" Foreground="Black" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Orange"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>

2.ControlTemplate 的Trigger

假如有以下需求:

  1.需要自定义一个IconButton,他有两个主要依赖属性:Icon和Header,属性类型为Object

  2.鼠标滑过IconButton时,可以修改背景色和前景色

代码:

<!--使用了Object和MouseOverObject两个对象,是为了处理图片的切换,比如美工针对鼠标进入和离开两种场景提供了两张图片,Header也类似,以及其他场景,如下:-->
<localControls:IconButton Height="" Width="" HeaderWidth="*"
Icon="MMM" MouseOverForeground="White" MouseOverBackground="Black">
<localControls:IconButton.Header>
<Border>
<TextBlock Text="asdfasdf"></TextBlock>
</Border>
</localControls:IconButton.Header>
<localControls:IconButton.MouseOverHeader>
<TextBlock Foreground="Orange" Text="MouseONver"></TextBlock>
</localControls:IconButton.MouseOverHeader>
</localControls:IconButton>

<Style x:Key="IconButtonStyle" TargetType="{x:Type localControls:IconButton}">
<!--Foreground这个属性,如果直接在IconButton对象上设置Foreground默认值,将导致Trigger无效,所以添加了NormalForeground-->
<Setter Property="NormalBackground" Value="Gray"></Setter>
<Setter Property="NormalForeground" Value="Black"></Setter>
<Setter Property="MouseOverBackground" Value="{Binding NormalBackground, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="MouseOverForeground" Value="{Binding NormalForeground, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="MouseOverIcon" Value="{Binding Icon, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="MouseOverHeader" Value="{Binding Header, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="DisabledBackground" Value="{Binding NormalBackground, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="DisabledForeground" Value="{Binding NormalForeground, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="DisabledIcon" Value="{Binding Icon, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="DisabledHeader" Value="{Binding Header, RelativeSource={RelativeSource Self}}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type localControls:IconButton}">
<Border x:Name="TemplateRoot" Background="{TemplateBinding NormalBackground}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{TemplateBinding IconWidth}"></ColumnDefinition>
<ColumnDefinition Width="{TemplateBinding HeaderWidth}"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="IconContentSite"
Content="{TemplateBinding Icon}"
Margin="{TemplateBinding IconMargin}"
HorizontalAlignment="{TemplateBinding IconHorizontalAlignment}"
VerticalAlignment="{TemplateBinding IconVerticalAlignment}"></ContentPresenter>
<ContentPresenter x:Name="HeaderContentSite" Grid.Column=""
Margin="{TemplateBinding HeaderMargin}"
HorizontalAlignment="{TemplateBinding HeaderHorizontalAlignment}"
VerticalAlignment="{TemplateBinding HeaderVerticalAlignment}"
Content="{TemplateBinding Header}"></ContentPresenter> </Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="IconContentSite" Property="Content" Value="{Binding MouseOverIcon,RelativeSource={RelativeSource TemplatedParent}}"></Setter>
<Setter TargetName="HeaderContentSite" Property="Content" Value="{Binding MouseOverHeader,RelativeSource={RelativeSource TemplatedParent}}"></Setter>
<Setter TargetName="TemplateRoot" Property="Background" Value="{Binding MouseOverBackground,RelativeSource={RelativeSource TemplatedParent}}"></Setter>
                <!--因为是要设置IconButton的依赖属性值,所以不设置TargetName,并且Model为Self,如果为TemplateParent,将无法绑定到目标值,具体原因看MSDN关于此枚举的介绍-->
<Setter Property="Foreground" Value="{Binding MouseOverForeground,RelativeSource={RelativeSource Self}}"></Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="false">
<Setter Property="Foreground" Value="{Binding NormalForeground,RelativeSource={RelativeSource Self}}"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="IconContentSite" Property="Content" Value="{Binding DisabledIcon,RelativeSource={RelativeSource TemplatedParent}}"></Setter>
<Setter TargetName="HeaderContentSite" Property="Content" Value="{Binding DisabledHeader,RelativeSource={RelativeSource TemplatedParent}}"></Setter>
<Setter TargetName="TemplateRoot" Property="Background" Value="{Binding DisabledBackground,RelativeSource={RelativeSource TemplatedParent}}"></Setter>
<Setter Property="Foreground" Value="{Binding DisabledForeground,RelativeSource={RelativeSource Self}}"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
 public class IconButton : Control
{
public IconButton()
{
} #region DependencyProperty
public object Icon
{
get { return (object)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
} // Using a DependencyProperty as the backing store for Icon. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(object), typeof(IconButton), new PropertyMetadata(null)); public Thickness IconMargin
{
get { return (Thickness)GetValue(IconMarginProperty); }
set { SetValue(IconMarginProperty, value); }
} // Using a DependencyProperty as the backing store for IconMargin. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconMarginProperty =
DependencyProperty.Register("IconMargin", typeof(Thickness), typeof(IconButton), new PropertyMetadata(new Thickness(0d))); public GridLength IconWidth
{
get { return (GridLength)GetValue(IconWidthProperty); }
set { SetValue(IconWidthProperty, value); }
} // Using a DependencyProperty as the backing store for IconWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconWidthProperty =
DependencyProperty.Register("IconWidth", typeof(GridLength), typeof(IconButton), new PropertyMetadata(GridLength.Auto)); public HorizontalAlignment IconHorizontalAlignment
{
get { return (HorizontalAlignment)GetValue(IconHorizontalAlignmentProperty); }
set { SetValue(IconHorizontalAlignmentProperty, value); }
} // Using a DependencyProperty as the backing store for IconHorizontalAlignment. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconHorizontalAlignmentProperty =
DependencyProperty.Register("IconHorizontalAlignment", typeof(HorizontalAlignment), typeof(IconButton), new PropertyMetadata(HorizontalAlignment.Center)); public VerticalAlignment IconVerticalAlignment
{
get { return (VerticalAlignment)GetValue(IconVerticalAlignmentProperty); }
set { SetValue(IconVerticalAlignmentProperty, value); }
} // Using a DependencyProperty as the backing store for IconVerticalAlignment. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IconVerticalAlignmentProperty =
DependencyProperty.Register("IconVerticalAlignment", typeof(VerticalAlignment), typeof(IconButton), new PropertyMetadata(VerticalAlignment.Center)); public object MouseOverIcon
{
get { return (object)GetValue(MouseOverIconProperty); }
set { SetValue(MouseOverIconProperty, value); }
} // Using a DependencyProperty as the backing store for MouseOverIcon. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverIconProperty =
DependencyProperty.Register("MouseOverIcon", typeof(object), typeof(IconButton), new PropertyMetadata(null)); public object DisabledIcon
{
get { return (object)GetValue(DisabledIconProperty); }
set { SetValue(DisabledIconProperty, value); }
} // Using a DependencyProperty as the backing store for DisabledIcon. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisabledIconProperty =
DependencyProperty.Register("DisabledIcon", typeof(object), typeof(IconButton), new PropertyMetadata()); public object Header
{
get { return (object)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
} // Using a DependencyProperty as the backing store for Header. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(object), typeof(IconButton), new PropertyMetadata(null)); public Thickness HeaderMargin
{
get { return (Thickness)GetValue(HeaderMarginProperty); }
set { SetValue(HeaderMarginProperty, value); }
} // Using a DependencyProperty as the backing store for HeaderMargin. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderMarginProperty =
DependencyProperty.Register("HeaderMargin", typeof(Thickness), typeof(IconButton), new PropertyMetadata(new Thickness(0d))); public HorizontalAlignment HeaderHorizontalAlignment
{
get { return (HorizontalAlignment)GetValue(HeaderHorizontalAlignmentProperty); }
set { SetValue(HeaderHorizontalAlignmentProperty, value); }
} // Using a DependencyProperty as the backing store for HeaderHorizontalAlignment. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderHorizontalAlignmentProperty =
DependencyProperty.Register("HeaderHorizontalAlignment", typeof(HorizontalAlignment), typeof(IconButton), new PropertyMetadata(HorizontalAlignment.Center)); public VerticalAlignment HeaderVerticalAlignment
{
get { return (VerticalAlignment)GetValue(HeaderVerticalAlignmentProperty); }
set { SetValue(HeaderVerticalAlignmentProperty, value); }
} // Using a DependencyProperty as the backing store for HeaderVerticalAlignment. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderVerticalAlignmentProperty =
DependencyProperty.Register("HeaderVerticalAlignment", typeof(VerticalAlignment), typeof(IconButton), new PropertyMetadata(VerticalAlignment.Center)); public GridLength HeaderWidth
{
get { return (GridLength)GetValue(HeaderWidthProperty); }
set { SetValue(HeaderWidthProperty, value); }
} // Using a DependencyProperty as the backing store for HeaderWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeaderWidthProperty =
DependencyProperty.Register("HeaderWidth", typeof(GridLength), typeof(IconButton), new PropertyMetadata(GridLength.Auto)); public object MouseOverHeader
{
get { return (object)GetValue(MouseOverHeaderProperty); }
set { SetValue(MouseOverHeaderProperty, value); }
} // Using a DependencyProperty as the backing store for MouseOverHeader. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverHeaderProperty =
DependencyProperty.Register("MouseOverHeader", typeof(object), typeof(IconButton), new PropertyMetadata(null)); public object DisabledHeader
{
get { return (object)GetValue(DisabledHeaderProperty); }
set { SetValue(DisabledHeaderProperty, value); }
} // Using a DependencyProperty as the backing store for DisabledHeader. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisabledHeaderProperty =
DependencyProperty.Register("DisabledHeader", typeof(object), typeof(IconButton), new PropertyMetadata(null)); public Brush MouseOverBackground
{
get { return (Brush)GetValue(MouseOverBackgroundProperty); }
set { SetValue(MouseOverBackgroundProperty, value); }
} // Using a DependencyProperty as the backing store for MouseOverBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverBackgroundProperty =
DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(IconButton), new PropertyMetadata(null)); public Brush MouseOverForeground
{
get { return (Brush)GetValue(MouseOverForegroundProperty); }
set { SetValue(MouseOverForegroundProperty, value); }
} // Using a DependencyProperty as the backing store for MouseOverForeground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MouseOverForegroundProperty =
DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(IconButton), new PropertyMetadata(new SolidColorBrush(Colors.Transparent))); public Brush NormalBackground
{
get { return (Brush)GetValue(NormalBackgroundProperty); }
set { SetValue(NormalBackgroundProperty, value); }
} // Using a DependencyProperty as the backing store for NormalBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NormalBackgroundProperty =
DependencyProperty.Register("NormalBackground", typeof(Brush), typeof(IconButton), new PropertyMetadata(null)); public Brush NormalForeground
{
get { return (Brush)GetValue(NormalForegroundProperty); }
set { SetValue(NormalForegroundProperty, value); }
} // Using a DependencyProperty as the backing store for NormalForeground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NormalForegroundProperty =
DependencyProperty.Register("NormalForeground", typeof(Brush), typeof(IconButton), new PropertyMetadata(null)); public Brush DisabledBackground
{
get { return (Brush)GetValue(DisabledBackgroundProperty); }
set { SetValue(DisabledBackgroundProperty, value); }
} // Using a DependencyProperty as the backing store for DisabledBackground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisabledBackgroundProperty =
DependencyProperty.Register("DisabledBackground", typeof(Brush), typeof(IconButton), new PropertyMetadata(null)); public Brush DisabledForeground
{
get { return (Brush)GetValue(DisabledForegroundProperty); }
set { SetValue(DisabledForegroundProperty, value); }
} // Using a DependencyProperty as the backing store for DisabledForeground. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DisabledForegroundProperty =
DependencyProperty.Register("DisabledForeground", typeof(Brush), typeof(IconButton), new PropertyMetadata(null));
#endregion }

WPF 使用Trigger遇到的问题的更多相关文章

  1. WPF 杂谈——Trigger触发器

    笔者在使用的WPF过程中,见过的触发器有三种:Trigger.DataTrigger.EventTrigger.其中最为常用的要属Trigger.至于触发器的作用就是当某个属性的值发生变化,应该去做某 ...

  2. 如何在 UWP 使用 wpf 的 Trigger

    本文需要告诉大家,如何使用 Behaviors 做出 WPF 的 Trigger ,需要知道 UWP 不支持 WPF 的 Trigger . 安装 Behaviors 请使用 Nuget 安装,可以输 ...

  3. WPF触发器(Trigger)

    WPF触发器(Trigger.DataTrigger.EventTrigger) WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改 ...

  4. WPF触发器(Trigger、DataTrigger、EventTrigger)

    WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...

  5. WPF 基础 - Trigger

    1. Trigger 1.1 由属性值触发的 Trigger 最基本的触发器,Property 是关注的属性名称,value 是触发条件,一旦触发条件满足,就会应用 Trigger 的 Setters ...

  6. WPF触发器(Trigger) - DataTrigger

    官方文档中对DataTrigger的介绍 Represents a trigger that applies property values or performs actions when the ...

  7. WPF XAML Trigger中使用动画后 动画对象冻结的处理办法

    在编写XAML时 在Trigger中使用动画,在动画之后,动画对象就会被冻结,无法被其他动画或者属性改变. 处理办法有: 1 使用附加属性来添加动画 public static readonly De ...

  8. 【WPF】使用 XAML 的 Trigger 系统实现三态按钮

    利用 WPF 的 Trigger 系统,也可以很简单的只使用xmal实现三态按钮.在Window或UserControl的资源中声明按钮的style并加入触发功能.使用的时候直接在button里复写s ...

  9. Trigger和ViewStateManager的具体比较

    ViewStateManager的好处  拥有 GeneratedDuration ,可以很方便的进行几个状态之间的切换过渡动画. 坏处是,在界面加载时只能显示默认效果,通过GoToStateActi ...

随机推荐

  1. 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(3)输入输出函数

    一.基本的输入和输出函数的用法 1.printf()  //屏幕输出 用法: (1)printf("字符串\n"); (2)printf("输出控制符", 输出 ...

  2. 具体分析contrex-A9的汇编代码__switch_to(进程切换)

    //函数原型:版本号linux-3.0.8 struct task_struct *__switch_to(structtask_struct *, struct thread_info *, str ...

  3. 微信公众号开发之怎样将本机IP映射成外网域名

    近期一个项目须要用到微信公众号的网页授权登录,在研究这个公众号的时候遇到各种困难,现将自己的一些心得总结一下. 我想进行微信公众号开发遇到的第一个困难就是微信公众号必须输入一个外网能够訪问的域名,在网 ...

  4. erlang 游戏服务器开发

    http://blog.csdn.net/slmeng2002/article/details/5532771 最近关注erlang游戏服务器开发  erlang大牛写的游戏服务器值得参考 介绍本文以 ...

  5. Activity启动模式需注意的坑

    标准启动Standard模式版本差异: 在Lollipop之前,每次以MULTIPLE启动的Activity都会被压入当前任务的顶部,启动 N 次,在当前任务就会出现 N 个Activity的实例,每 ...

  6. NOIP模拟 Math - 数学

    题目大意: 给定a,n(\(a \le 1e9, n\le30\)),求有多少\(b(1 \le b \le 2^n)\)满足:\(a^b \equiv b^a(mod 2^n)\). 题目分析: 数 ...

  7. 【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 [题意] [题解] 预开一个20W长度的线段树; 这里a[1..20W]={0} ...

  8. 全局获取Context的技巧(再也不要为获取Context而感到烦恼)

    1.Context概念 Context,相信不管是第一天开发Android,还是开发Android的各种老鸟,对于Context的使用一定不陌生~~你在加载资源.启动一个新的Activity.获取系统 ...

  9. Apache2.4.25 VirtualHost rewrite_module

    LoadModule rewrite_module libexec/apache2/mod_rewrite.so Include /private/etc/apache2/extra/httpd-vh ...

  10. 警告异常:Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5

    1.打开 regedit.exe 注册表编辑器 2.找出文件名称 HKEY_CURRENT_USER\Software\JavaSoft 和 HKEY_LOCAL_MACHINE\SOFTWARE\J ...