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)); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)