silverlight - 更改 TargetNullValue 的 TextBlock 样式

标签 silverlight xaml silverlight-5.0

如果绑定(bind)属性的值为空,我想更改 TextBlock 的样式。我已经为要显示的 TextBlock 的 TargetNullValue 指定了一个值,但我想用另一种样式显示它。我该怎么做。

我目前的解决方案是使用两个 TextBlocks 并控制两者的可见性,在原始风格和替代风格之间切换。但是这个解决方案不可行,因为我需要复制每个 TextBlock,以显示替代版本。

当前解决方案:

<TextBlock Visibility="{Binding MyText, Converter={StaticResource nullToVisibilityConverter}}"
           FontSize="20"
           Foreground="Black"
           Text="{Binding MyText}" />

<TextBlock Visibility="{Binding MyText, Converter={StaticResource nullToVisibilityConverter}}"
           FontSize="20"
           FontStyle="Italic"
           Foreground="Gray"
           Text="None" />

需要的解决方案:
<TextBlock FontSize="20"
           Foreground="Black"
           Text="{Binding MyText, TargetNullValue='None'}" />

<!-- plus any styles, templates or triggers, to change style of TextBlock for TargetNullValue -->

我如何为 TargetNullValue 使用另一种样式。欢迎使用任何使用样式、触发器或模板的解决方案。

最佳答案

注意:这适用于 WPF,您可能必须转换任何与 SL5.0 不完全吻合的东西。

最简单的解决方案(除非此要求无处不在)是为每个实例创建一个特定的样式,该样式绑定(bind)到与文本 block 相同的属性,检查 Null 并在那里设置属性。

这个例子将很好地粘贴到 Kaxaml 中。



  <Style x:Key="tacoStyle" TargetType="TextBlock">
    <Style.Triggers>
      <DataTrigger Binding="{Binding Source={StaticResource taco}}" Value="{x:Null}">
        <Setter Property="Foreground" Value="Red"/>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</StackPanel.Resources>
<TextBlock Style="{StaticResource tacoStyle}" Text="{Binding Source={StaticResource taco}, TargetNullValue='bacon'}"/>

当然,如果您需要更通用的解决方案,您可以扩展 TextBlock并添加一些逻辑来处理这个问题。
<StackPanel>
        <StackPanel.Resources>
            <x:NullExtension x:Key="taco"/>
            <Style x:Key="AltTacoStyle" TargetType="yourNS:ExtendedTextBlock">
                <Setter Property="Foreground" Value="Pink"/>
            </Style>
        </StackPanel.Resources>
        <yourNS:ExtendedTextBlock Text="{Binding Source={StaticResource taco}, TargetNullValue='bacon'}"
                                                                                AltStyle="{StaticResource AltTacoStyle}"
                                                                                AllowAltStyleOnNull="True"/>
    </StackPanel>

和控制:
    public class ExtendedTextBlock : TextBlock {


    public static readonly DependencyProperty AllowAltStyleOnNullProperty =
        DependencyProperty.Register("AllowAltStyleOnNull", typeof (bool), typeof (ExtendedTextBlock), new PropertyMetadata(default(bool)));

    public bool AllowAltStyleOnNull {
        get { return (bool) GetValue(AllowAltStyleOnNullProperty); }
        set { SetValue(AllowAltStyleOnNullProperty, value); }
    }

    public static readonly DependencyProperty AltStyleProperty =
        DependencyProperty.Register("AltStyle", typeof (Style), typeof (ExtendedTextBlock), new PropertyMetadata(default(Style)));

    public Style AltStyle {
        get { return (Style) GetValue(AltStyleProperty); }
        set { SetValue(AltStyleProperty, value); }
    }

    static ExtendedTextBlock() {
        TextProperty.OverrideMetadata(typeof(ExtendedTextBlock), new FrameworkPropertyMetadata(default(string), PropertyChangedCallback));
    }

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) {
        var tb = (ExtendedTextBlock)dependencyObject;
        var binding = tb.GetBindingExpression(TextProperty);
        if (binding != null && binding.DataItem == null) {
            if (tb.AllowAltStyleOnNull)
                tb.Style = tb.AltStyle;
        }
    }
}

您需要稍微充实一下,以便为生产做好准备,但您明白了。

关于silverlight - 更改 TargetNullValue 的 TextBlock 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9531249/

相关文章:

silverlight - 如何在 Silverlight 中动态切换数据绑定(bind)的 UI 文化

silverlight - 如何检测断开的双工轮询客户端

c# - 使用 WrapPanel 指定每行的项目数

silverlight - 如何使用 Silverlight 访问 PostgreSQL

c# - WPF 直接绑定(bind)到 DataContext

c# - 从 F# 访问 C# WPF

c# - 即使在清除 RichTextbox 后,段落仍显示为另一个元素的子元素

asp.net - 在打开 Silverlight 项目之前,您需要安装最新的 Silverlight 开发人员运行时

c# - WPF ComboboxItem 绑定(bind)

c# - Silverlight 中的对象深拷贝