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)); }
作者:唐宋元明清2188
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。