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));
    }
复制代码

 

posted @   唐宋元明清2188  阅读(241)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示
哥伦布
01:11发布
哥伦布
01:11发布
8°
西北风
2级
空气质量
相对湿度
81%
今天
中雨
3°/15°
周四
大雨
2°/14°
周五
小雨
4°/10°