2012-03-02 6 views
4

バインドされたプロパティの値がnullの場合、TextBlockのStyleを変更したいと思います。表示するTextBlockのTargetNullValueの値を指定しましたが、代替スタイルで表示する必要があります。どうすればいいの?TargetNullValueのTextBlockのスタイルを変更する

私の現在の解決策は、2つのTextBlockを使用して両方の可視性を制御して、元のスタイルと代替スタイルを切り替えます。しかし、この解決策は実行可能ではありません。代替テキストを表示するために、各TextBlockを複製する必要があるためです。

現在のソリューション:

<TextBlock FontSize="20" 
      Foreground="Black" 
      Text="{Binding MyText, TargetNullValue='None'}" /> 

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

どのように私はTargetNullValueためalternativスタイルを使用することができますソリューションを必要とし

<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" /> 

。スタイル、トリガー、またはテンプレートを使用するソリューションはすべて歓迎します。

答えて

1

注:これはWPF用です.SL5.0と完全に絡み合わないものを変換する必要があります。

最も簡単な解決策(この要件が普及していない限り)は、テキストブロックと同じプロパティにバインドし、そこでプロパティを設定し、各インスタンスに特定のスタイルを設定することです。

この例は、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; 
     } 
    } 
} 

あなたは生産準備ができて少しそれを肉付けする必要がありますが、あなたのアイデアを得ます。

関連する問題