1. 程式人生 > >MultiTigger 綁定異常處理

MultiTigger 綁定異常處理

res pan keyframes depend horizon mar ins oid setter

原文: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 綁定異常處理