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.