我必须将以下样式应用于我的ListViewItem
<UserControl.Resources>
<local:Look x:Key="ListViewItemLook" Background="Magenta"/>
<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{Binding Source={DynamicResource ListViewItemLook}, Path=Background}"/>
</Trigger>
</Style.Triggers>
</Style>但是我得到了一个例外,我试着改变:
<Setter Property="Background" Value="{Binding Path=Background}"/>并添加到样式中:
<Setter Property="DataContext" Value="{DynamicResource ListViewItemLook}"/>但是is不起作用。我不能绑定到运行时,因为我需要设置BackGround属性StaticResource -time。
我该怎么办?谢谢。
发布于 2012-10-02 05:10:46
将颜色拉出到单独的SolidColorBrush中,并使两个项目都引用它:
<SolidColorBrush x:Key="SelectedListViewItemBackground" Color="Magenta" />
<local:Look x:Key="whatever" Background="{StaticResource SelectedListViewItemBackground}" />
<Setter Property="Background" Value="{StaticResource SelectedListViewItemBackground}" />如果你想做其他的事情,我不知道它是什么,因为这个问题没有意义。
发布于 2012-10-03 00:57:46
据我所知,DynamicResource扩展使用DependencyProperty机制(非常类似于绑定)。因此,不能使用DynamicResource设置Binding对象的Source属性,因为它不是DependencyProperty。
此外,如果您想在资源中更改Look的Background属性而不是外观本身,那么将Look as a static resource设置为binding属性应该不成问题。当然,Look类的后台属性要么触发一个PropertyChanged事件,要么本身就是一个DependencyProperty。
https://stackoverflow.com/questions/12680237
复制相似问题