风格有不同绑定一个DataTrigger [英] Style with a DataTrigger that has different Bindings

查看:195
本文介绍了风格有不同绑定一个DataTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系列,我想应用样式和DataTrigger的TextBlocks的。唯一的区别是,该结合是在为每一个视图模型不同的属性

I have a series of TextBlocks to which I want apply a Style and DataTrigger. The only difference is that the Binding is to a different property in the view model for each one.

下面是的TextBlocks中的一个的简化的版本与风格和DataTrigger 建在

Here is a simplified version of one of the TextBlocks with the Style and DataTrigger "built in".

        <TextBlock Text="Is development">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsDevelopmentIsDirty}"
                                      Value="True">
                            <Setter Property="FontWeight"
                                    Value="Bold" />
                            <Setter Property="FontStyle"
                                    Value="Italic" />
                            <Setter Property="Foreground"
                                    Value="{StaticResource SCB_TardisBlue}" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>



所以,在前面的示例中,IsDevelopmentIsDirty的结合将是每个TextBlock的不同。

So, in the preceding example, the "IsDevelopmentIsDirty" binding would be different for each TextBlock.

我只是没有找到最好的方式来巩固这种风格为可被整个系列的TextBlocks的使用的一个声明中最大的幸运。

I'm just not having the greatest luck in finding the best way to consolidate this Style into one declaration that can be used by the entire series of TextBlocks.

有没有分配样式和该DataTrigger应绑定属性的方法吗?如果不是,有什么办法做到这一点?我在此先感谢。

Is there a way to assign the style and the property to which the DataTrigger should bind? If not, what is a way to do this? My thanks in advance.

推荐答案

如果您要根据视的TextBlock 您可以使用代理属性。这是假设它会在布尔属性,或者一个可以转换为布尔。

If you want to trigger same style change based on different property depending on the TextBlock you can use proxy property. This is assuming that it will a boolean property, or one that can be converted to boolean.

解决方案1 ​​

在最简单的解决方案,您可以使用标签属性

In simplest solution you can use Tag property

<TextBlock Text="Is development" Tag="{Binding IsDevelopmentIsDirty}" >
   <TextBlock.Style>
      <Style TargetType="{x:Type TextBlock}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="True">
               <Setter Property="FontWeight" Value="Bold" />
               <Setter Property="FontStyle" Value="Italic" />
               <Setter Property="Foreground" Value="{StaticResource SCB_TardisBlue}" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </TextBlock.Style>
</TextBlock>



样式放在你触发标签来任何你想要的财产为true,然后在外面可以绑定标签属性。当然,这风格然后可以提取到一些资源字典。

From inside of the Style you trigger on Tag property being true and then on the outside you can bind Tag property to whatever you want. Of course this Style can then be extracted to some resource dictionary.

解决方案2

例如另一种解决方案,如果你需要更多的一个属性是创建附加属性。

Another solution for example if you need more the one property is to create attached properties

public static class AttachedProperties
{
    public static readonly DependencyProperty ChangeStyleProperty = DependencyProperty.RegisterAttached("ChangeStyle", typeof(bool), typeof(AttachedProperties));

    public static bool GetChangeStyle(DependencyObject d)
    {
        return (bool)d.GetValue(ChangeStyleProperty);
    }

    public static void SetChangeStyle(DependencyObject d, bool value)
    {
        d.SetValue(ChangeStyleProperty, value);
    }
}



,然后按照同样的场景与标签

<TextBlock Text="Is development" prop:AttachedProperties.ChangeStyle="{Binding IsDevelopmentIsDirty}" >
   <TextBlock.Style>
      <Style TargetType="{x:Type TextBlock}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(prop:AttachedProperties.ChangeStyle)}" Value="True">
               <Setter Property="FontWeight" Value="Bold" />
               <Setter Property="FontStyle" Value="Italic" />
               <Setter Property="Foreground" Value="{StaticResource SCB_TardisBlue}" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </TextBlock.Style>
</TextBlock>

这篇关于风格有不同绑定一个DataTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆