【问题标题】:Sharing Style with DataTrigger depending on bound value根据绑定值与 DataTrigger 共享样式
【发布时间】:2017-08-22 13:31:55
【问题描述】:

我已搜索但尚未找到任何方法来执行此操作。我对 XAML 和绑定也比较陌生。

我有很多TextBlocks,其中的文本绑定到一个属性并且有一个StringFormat:

<TextBlock Text="{Binding PropA, StringFormat=Running {0}"/>
<TextBlock Text="{Binding Height, StringFormat=Sub: \{0\}"/>
<TextBlock Text="{Binding Depth, StringFormat=Indie: \{0\}"/>
<TextBlock Text="{Binding Color, StringFormat=State: \{0\}"/>

现在我想根据绑定变量的值应用样式(因此显示的真实文本无关紧要)。目前我必须对每个元素应用 &lt;Style&gt; 并再次直接使用绑定属性。这看起来很麻烦并且容易出现复制粘贴错误。我想知道我是否可以定义一个&lt;Style&gt; 并分享它。

类似:

<Style x:Key="Highlight_Non_Zero">
  <!-- invert the logic because DataTrigger is an equality comparer -->
  <Setter Property="TextBlock.Background" Value="Orange"/>
  <Style.Triggers>
    <DataTrigger Binding="{Binding <some magic here that gets the element this style applies to bound variable>" Value="0">
      <Setter Property"TextBlock.Background" Value="Transparent"/>
    </DataTrigger>
  </Style.Triggers>
</Style>

<TextBlock Text="{Binding PropA, StringFormat=Running {0}" Style="{StaticResource Highlight_Non_Zero}"/>
<TextBlock Text="{Binding Height, StringFormat=Sub: \{0\}" Style="{StaticResource Highlight_Non_Zero}"/>
<TextBlock Text="{Binding Depth, StringFormat=Indie: \{0\}" Style="{StaticResource Highlight_Non_Zero}"/>
<TextBlock Text="{Binding Color, StringFormat=State: \{0\}" Style="{StaticResource Highlight_Non_Zero}"/>

【问题讨论】:

    标签: c# xaml data-binding


    【解决方案1】:

    您可以使用附加属性将一些常见(例如bool 值)传递给样式:

    public class TextBlockBehavior
    {
        public static bool GetMyProperty(DependencyObject obj) => (bool)obj.GetValue(MyPropertyProperty);
        public static void SetMyProperty(DependencyObject obj, bool value) => obj.SetValue(MyPropertyProperty, value);
    
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.RegisterAttached("MyProperty", typeof(bool), typeof(TextBlockBehavior), new PropertyMetadata(false));
    }
    

    然后每个TextBlock 可以从不同的来源设置它的值:

    <TextBlock Text="{Binding PropA, StringFormat=Running \{0\}}"
               local:TextBlockBehavior.MyProperty="{Binding PropA ...}"
               Style="{StaticResource Highlight_Non_Zero}" />
    

    注意:您必须使用转换器或 ViewModel 中的其他属性来制作各种类型的属性(在您的情况下为 PropA、Color、Depth 等)设置值为bool MyProperty。

    然后你就可以在风格上使用它了:

    <Style x:Key="Highlight_Non_Zero" TargetType="TextBlock">
        <Setter Property="TextBlock.Background" Value="Orange" />
        <Style.Triggers>
            <Trigger Property="local:TextBlockBehavior.MyProperty" Value="True">
                <Setter Property="TextBlock.Background" Value="Transparent" />
            </Trigger>
        </Style.Triggers>
    </Style>
    

    如果样式触发做很多事情,给定的解决方案非常适合。在您的情况下(更改背景),为每种类型简单地制作转换器(或使用 ViewModel 中的另一个属性,颜色可以返回,参见例如this)以产生所需的背景画笔/颜色(例如@987654331)会更容易@):

    <TextBlock Text="{Binding PropA, StringFormat=Running \{0\}}"
               Background="{Binding PropA, Converter=local:SomeTypeToBackground}" />
    

    【讨论】:

      猜你喜欢
      • 2013-07-10
      • 2013-09-05
      • 1970-01-01
      • 2017-01-23
      • 2011-10-09
      • 2023-01-13
      • 1970-01-01
      • 2019-04-29
      • 2015-06-29
      相关资源
      最近更新 更多