MultiTigger 绑定异常处理
异常产生环境:
在初始化一个窗口后,没有show出来。在此窗口中,有个控件,重写了控件模板,并加了MultiTrigger。
注意:俩个Condition,一个是从外面绑定过来的Tag,一个是ControlTemplate中Element的属性Tag。
因为有时候控件自带的Tag值不够使用,因此需要另一个Tag来支持Trigger里面的逻辑。
    <ControlTemplate TargetType="{x:Type p:InteractiveButton}">
        <Grid x:Name="RootGrid" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
            <Border Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ContentPresenter ContentSource="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
        <Image x:Name="Image5" Visibility="Collapsed" Source="{StaticResource Image.Titlebar.NewMessageNote}"/>
            </Border>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="Tag" Value="Searched">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Image5" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                            </ObjectAnimationUsingKeyFrames>
                            <DoubleAnimation Storyboard.TargetName="Image5" Storyboard.TargetProperty="Height"
                                             From="2" To="15" Duration="0:0:0.2"></DoubleAnimation>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Tag" Storyboard.TargetProperty="Tag">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="NewMessageAnmation1" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <Trigger SourceName="RootGrid" Property="Tag" Value="NewMessageAnmation1">
                <Trigger.EnterActions>
                    <BeginStoryboard Name="BreatheStoryboard">
                        <Storyboard DesiredFrameRate="20">
                            <DoubleAnimation Storyboard.TargetName="Image5" Storyboard.TargetProperty="Height"
                                             From="10" To="2" RepeatBehavior="Forever" AutoReverse="True" Duration="0:0:0.68"></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Tag" Value="Searching" />
                    <Condition SourceName="RootGrid" Property="Tag" Value="NewMessageAnmation1” />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#F4F4F4" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <Button Tag={Binding Type}/>
然后在另一窗口或者后台线程中,添加了PropertyChanged的属性Type,值改变时
private string _type = string.Empty;
public string Type
{
get { return _type; }
set
{
_type = value;
RaisePropertyChanged(nameof(Type));
}
}
引进上面的MultiTrigger中一个Condition 值变化,但是另一个Condition和Setter(Actions)引用了ControlTemplate中的Eelement,这时会引发
未将对象引用到实例

如上异常,解决方案:
用附加属性替代 SourceName="RootGrid" Property="Tag" .即可
    <ControlTemplate TargetType="{x:Type p:InteractiveButton}">
        <Grid x:Name="RootGrid" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
            <Border Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ContentPresenter ContentSource="{TemplateBinding Content}" Margin="{TemplateBinding Padding}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
        <Image x:Name="Image5" Visibility="Collapsed" Source="{StaticResource Image.Titlebar.NewMessageNote}"/>
            </Border>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="Tag" Value="Searched">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Image5" Storyboard.TargetProperty="Visibility">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                            </ObjectAnimationUsingKeyFrames>
                            <DoubleAnimation Storyboard.TargetName="Image5" Storyboard.TargetProperty="Height"
                                             From="2" To="15" Duration="0:0:0.2"></DoubleAnimation>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(ui:TitlebarHelper.Type)">
                                <DiscreteObjectKeyFrame KeyTime="0:0:0.2" Value="NewMessageAnmation1" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="ui:TitlebarHelper.Type" Value="NewMessageAnmation1">
                <Trigger.EnterActions>
                    <BeginStoryboard Name="BreatheStoryboard">
                        <Storyboard DesiredFrameRate="20">
                            <DoubleAnimation Storyboard.TargetName="Image5" Storyboard.TargetProperty="Height"
                                             From="10" To="2" RepeatBehavior="Forever" AutoReverse="True" Duration="0:0:0.68"></DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Tag" Value="Searching" />
                    <Condition Property="ui:TitlebarHelper.Type" Value="NewMessageAnmation1” />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="#F4F4F4" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <Button Tag={Binding Type}/>
public static class TitlebarTypeHelper
{
public static string GetType(DependencyObject obj)
{
return (string)obj.GetValue(TypeProperty);
} public static void SetType(DependencyObject obj, string value)
{
obj.SetValue(TypeProperty, value);
} /// <summary>
/// 附加属性
/// </summary>
public static readonly DependencyProperty TypeProperty =
DependencyProperty.RegisterAttached("Type", typeof(string), typeof(TitlebarTypeHelper),
new PropertyMetadata(null));
}
MultiTigger 绑定异常处理的更多相关文章
- Laravel 流程分析——应用程序初始化
		
在整体分析中,我们看到Laravel首先会进行一个app的初始化,代码如下: $app = require_once __DIR__.'/../bootstrap/app.php'; 我们具体看看ap ...
 - Android系统的“程序异常退出”[转]
		
在应用运行过程中,有很多异常可能会发生,而我们希望在异常发生的时候第一时间的保存现场. 如何处理未捕获的异常呢? 首先我们要实现一个接口 java.lang.Thread.UncaughtExcep ...
 - Laravel源码解析--看看Lumen到底比Laravel轻在哪里
		
在前面一篇<Laravel源码解析--Laravel生命周期详解>中我们利用xdebug详细了解了下Laravel一次请求中到底做了哪些处理.今天我们跟 Lumen 对比下,看看 Lume ...
 - Android使用UncaughtExceptionHandler捕获全局异常
		
Android系统的“程序异常退出”,给应用的用户体验造成不良影响.为了捕获应用运行时异常并给出友好提示,便可继承UncaughtExceptionHandler类来处理.通过Thread.setDe ...
 - JAVAEE——SpringMVC第二天:高级参数绑定、@RequestMapping、方法返回值、异常处理、图片上传、Json交互、实现RESTful、拦截器
		
1. 课前回顾 https://www.cnblogs.com/xieyupeng/p/9093661.html 2. 课程计划 1.高级参数绑定 a) 数组类型的参数绑定 b) List类型的绑定 ...
 - Spring MVC温故而知新 – 参数绑定、转发与重定向、异常处理、拦截器
		
请求参数绑定 当用户发送请求时,根据Spring MVC的请求处理流程,前端控制器会请求处理器映射器返回一个处理器,然后请求处理器适配器之心相应的处理器,此时处理器映射器会调用Spring Mvc 提 ...
 - 05 SpringMVC:02.参数绑定及自定义类型转换&&04.SpringMVC返回值类型及响应数据类型&&05.文件上传&&06.异常处理及拦截器
		
springMVC共三天 第一天: 01.SpringMVC概述及入门案例 02.参数绑定及自定义类型转换 03.SpringMVC常用注解 第二天: 04.SpringMVC返回值类型及响应数据类型 ...
 - Spring 4 异常处理
		
异常与HTTP状态码的映射(@ResponseStatus) Spring默认会将自身抛出的异常自动映射到合适的状态码,如下是一些示例: 举个例子,当后端抛出如下异常(TypeMismatchExce ...
 - 七天学会ASP.NET MVC (六)——线程问题、异常处理、自定义URL
		
本节又带了一些常用的,却很难理解的问题,本节从文件上传功能的实现引出了线程使用,介绍了线程饥饿的解决方法,异常处理方法,了解RouteTable自定义路径 . 系列文章 七天学会ASP.NET MVC ...
 
随机推荐
- emqtt 试用(二)验证 emq 和 mosquito 的共享订阅
			
本地订阅(Local Subscription) 本地订阅(Local Subscription)只在本节点创建订阅与路由表,不会在集群节点间广播全局路由,非常适合物联网数据采集应用. 使用方式: 订 ...
 - ELK学习总结(3-3)elk的组合查询
			
1.bool 查询: must: 必须 should: 可以满足,也可以不满足. must_not: minimum_should_match: 至少要x个匹配才算匹配成功 disable_coor ...
 - git常用命令行总结
			
Git是当今最流行的版本控制工具.这几年GitHub也干掉了GoogleCode和Sourceforge,从三大代码仓库中脱颖而出,除了GitHub自身的优秀外,Git也是功不可没. 为何Git如此出 ...
 - VS2013 工程属性配置
			
1. 配置属性设置 设置工程编译输出目录 2. 设置第三方库的头文件的位置 3.设置第三方库(动态库或者静态库链接的搜寻的目录) 4.设置链接的第三方库的名称 注: 第三方库的链接可以通过配置文件来实 ...
 - 如何用elementui去实现图片上传和表单提交,用axios的post方法
			
下面是在vue搭建的脚手架项目中的组件component文件夹下面的upload.vue文件中的内容 <!--这个组件主要用来研究upload这个elementui的上传插件组件--> & ...
 - Frame
			
Frame意为框架,是在屏幕上的一个矩形区域. Frame主要作为其他组件的框架基础,或为其他组件提供间距补充. 何时使用Frame组件呢? Frame组件主要用于在复杂的布局中奖其他组件分组,也用于 ...
 - Visual Studio的一些快捷键
			
1)CTRL + W选择当前单词 2) Ctrl+F10: 运行到光标处 1.CTRL + SHIFT + B生成解决方案 2.CTRL + F7 生成编译 3. CTRL + O 打开文件 ...
 - 【python进阶】详解元类及其应用2
			
前言 在上一篇文章[python进阶]详解元类及其应用1中,我们提到了关于元类的一些前置知识,介绍了类对象,动态创建类,使用type创建类,这一节我们将继续接着上文来讲~~~ 5.使⽤type创建带有 ...
 - Random Forest vs GradientBoostingDecisionTree
			
相同 随机森林和GBDT都属于集成算法,base model都是决策树. 不同 随机森林 随机森林是决策树的bagging. bagging通过重复对原训练数据集上进行有放回地采样生成的数据集用bas ...
 - 实验吧_简单的sql注入_1、2、3
			
简单的sql注入1 看着这个简单的界面,一时间没有特别好的思路,先输入一个1',发生了报错 初步猜测这是一个字符型的注入,他将我们输入的语句直接当成sql语句执行了,按题目的意思后面肯定过滤了很多注入 ...