WPF Background property does not point to a dependencyobject in path '(0).(1)'
来源:互联网 发布:数据库用户老是被锁 编辑:程序博客网 时间:2024/06/06 04:01
使用 <ListBox.ItemTemplate> 制定 TabItem 模板时,想要用动画给 TabItem 加上闪烁效果。 代码如下:
<Border BorderBrush="#FFAAAAAA" BorderThickness="0,0,0,1" Background="White"><Border.Style><Style TargetType="Border"><Style.Triggers><DataTrigger Binding="{Binding Path=Quoted}" Value="true"><DataTrigger.EnterActions><BeginStoryboard Name="OperationStatusStoryboard"><Storyboard FillBehavior="Stop"><ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="White" To="Pink" Duration ="0:0:0.125" AutoReverse="True" RepeatBehavior="Forever" /></Storyboard></BeginStoryboard></DataTrigger.EnterActions></DataTrigger><DataTrigger Binding="{Binding Path=Quoted}" Value="false"><DataTrigger.EnterActions><RemoveStoryboard BeginStoryboardName="OperationStatusStoryboard"></RemoveStoryboard></DataTrigger.EnterActions></DataTrigger></Style.Triggers></Style></Border.Style>
在 ListBox 第一次刷出来时就会报 Background property does not point to a dependencyobject in path '(0).(1)'
但是 Style 的 TargetType 明明是 Border, 没理由找不到 Background 属性。 第一次刷完之后动画效果都没问题。 也就是说在 ListBox Show() 之前, ColorAnimation 找不到 Border. 也就是说它可能只是沿着逻辑树往上找到 Style 对象, 然后试图把 Background 绑到 Style 对象上。 我们知道 Style 直接继承自 DispatcherObject, 它不是 DependencyObject,(这也是为什么Style 不能 BaseOn DynamicResource。) 更不是一个 Visual 子类。 那么问题就变成 ColorAnimation 什么时候能够找到 Border 对象。Style 设置的属性值什么时候能够应用到 Border 上面? 从 UI 的行为来看, 很明显只有 Border.Loaded() 事件完成之后属性值才能被应用。 当然这一点有待查证。
由于我的 XAML 没有 cs 代码, 我们应用 MVVMLight, 希望能够把界面编码都交给美工。 所以从 Loaded() 事件着手解决这个问题就违反 MVVMLight 原则. 从 Stack Over Flow 找到一个解决方案:
<Border BorderBrush="#FFAAAAAA" BorderThickness="0,0,0,1"> <Border.Style> <Style TargetType="Border"> <Setter Property="Background"> <Setter.Value> <SolidColorBrush Color="White"/> </Setter.Value> </Setter> <Style.Triggers> <DataTrigger Binding="{Binding Path=Quoted}" Value="true"> <DataTrigger.EnterActions> <BeginStoryboard Name="OperationStatusStoryboard"> <Storyboard FillBehavior="Stop"> <ColorAnimation Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" From="White" To="Pink" Duration ="0:0:0.125" AutoReverse="True" RepeatBehavior="Forever" /> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> </DataTrigger> <DataTrigger Binding="{Binding Path=Quoted}" Value="false"> <DataTrigger.EnterActions> <RemoveStoryboard BeginStoryboardName="OperationStatusStoryboard"></RemoveStoryboard> </DataTrigger.EnterActions> </DataTrigger> </Style.Triggers> </Style> </Border.Style>
完美解决这个问题, 但是具体原因还需要研究。
写 xaml 时, 一个麻烦的问题在于, Style, ContentTemplate, DataTemplate 还有 ViewModel 之间的数据关系。 通常说来在 Style, ContentTemplate, DataTemplate 需要ViewModel 的数据时, 直接用 binding path="", 即可。 但是要在控件中使用 Style, ContentTemplate, DataTemplate 时, 由于Style 和模板通常是在独立的逻辑树里面, 如果需要在样式和模板中使用控件上的属性时, 必须通过 RelativeSource 来访问。 如下代码:
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}"> <Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/> </Trigger> </Style.Triggers></Style>
RelativeSource 有六个公开属性, 每种对应一个查找相对资源的路径, 参考: CSDN RelativeSource
- WPF Background property does not point to a dependencyobject in path '(0).(1)'
- Path does not point to a directory.
- Project property "XFire services.xml path" does not refer to a valid file解决办法
- Project property "XFire services.xml path" does not refer to a valid file问题解决
- repo sync 出错:does not point to a valid object
- does not point to a valid jvm installation
- eclipse3.1.2 +tomcat5.0 配置问题:Unable to find a javac compiler ;Perhaps JAVA_HOME does not point to the JDK
- WPF error: does not contain a static 'Main' method suitable for an entry point
- WPF error: does not contain a static 'Main' method suitable for an entry point
- WPF 错误:DependencyProperty.UnsetValue}' is not a valid value for property 'Background'
- JAVA_HOME does not point to the JDK
- Unable to find a javac compiler;Perhaps JAVA_HOME does not point to the JDK
- Unable to find a javac compiler;Perhaps JAVA_HOME does not point to the JDK解决
- Ant Unable to find a javac compiler Perhaps JAVA_HOME does not point to the JDK 解决办法
- jdk的设置及安装android studio提示does not point to a valid jvm问题
- Android Studio 启动问题<does not point to a valid jvm installation>错误
- 打开 android studio 时遇到does not point to a valid
- the environment variable java_home (with the value of ) does not point to a valid jvm installation
- ListView不能响应itemclick事件的解决方法
- netstat命令参数详解
- select机制
- getHibernateTemplate().find()方法
- mysql子查询更新问题
- WPF Background property does not point to a dependencyobject in path '(0).(1)'
- maven打war包排除文件目录
- lua之协同程序coroutine
- Spring MVC 教程,快速入门,深入分析
- cakephp2.X 多表联查 join 使用分页
- maven打包web工程中一部分代码为jar
- Linux内核中C语言使用特点和技巧
- makefile编译的时候undefined reference问题总结
- RTSP介绍