MultiTigger 绑定异常处理

本文介绍了一个在WPF应用程序中使用MultiTrigger时遇到的问题及解决方案。问题发生在控件模板中,当其中一个条件引用控件模板内部元素的属性时,可能会导致运行时错误。文章详细解释了解决方案,即通过使用附加属性来避免此类异常。

异常产生环境:

在初始化一个窗口后,没有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));
    }

 

转载于:https://www.cnblogs.com/kybs0/p/7287128.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值