Change TextBlock Style for TargetNullValue

I want to change the TextBlock style if the value of the bound property is null. I have specified a value for the TargetNullValue TextBlock to display, but I want to display it with an alternate style. How can i do this.

My current solution is to use two TextBlocks and control the visibility of both, to switch between the original and alternative styles. But this solution is not practical, since I need to duplicate each TextBlock to display an alternative version.

Current solution:

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

Necessary solution:

 <TextBlock FontSize="20" Foreground="Black" Text="{Binding MyText, TargetNullValue='None'}" /> <!-- plus any styles, templates or triggers, to change style of TextBlock for TargetNullValue --> 

How can I use an alternate style for TargetNullValue. Any solutions using styles, triggers or templates are welcome.

+4
source share
1 answer

Note. This is for WPF, you may need to convert everything that is not quite with the SL5.0 network.

The simplest solution (if this requirement is not ubiquitous) is to create a specific style for each instance, which is associated with the same property as the text block, checks for Null and sets the properties there.

This example will fit well with 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'}"/> 

Of course, if you need a more general solution, you can extend the TextBlock and add some logic to handle this.

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

And management:

  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; } } } 

You will need a little to be ready for production, but you will get this idea.

+1
source

Source: https://habr.com/ru/post/1399362/


All Articles