2012-03-02 28 views
4

如果bound屬性的值爲null,我想更改TextBlock的樣式。我爲要顯示的TextBlock的TargetNullValue指定了一個值,但我想用另一種樣式顯示它。我該怎麼做。爲TargetNullValue更改TextBlock的樣式

我目前的解決方案是使用兩個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。歡迎任何使用樣式,觸發器或模板的解決方案。

回答

1

注意:這是針對WPF的,您可能必須將任何與SL5.0不完全匹配的內容進行轉換。

最簡單的解決方案(除非此要求無處不在)是爲每個與文本塊綁定相同屬性的實例創建特定樣式,檢查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; 
     } 
    } 
} 

你需要充實說出來有點是生產準備,但你的想法。