TextBlock style triggers

I would like to combine DisplayNames from two different ViewModels, but only if the first is not NullObject.

I could easily do this in a converter or parent view model, but I hope that my attempt to use a DataTrigger has an easy fix.

Cheers, Berryl

This does not display anything:

<TextBlock Grid.Column="2" Grid.Row="0" > <TextBlock.Inlines> <Run Text="{Binding HonorificVm.DisplayName}"/> <Run Text="{Binding PersonNameVm.DisplayName}"/> </TextBlock.Inlines> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding HonorificVm.Honorific}" Value="{x:Static model:Honorific.NullHonorific}"> <Setter Property="Text" Value="PersonNameVm.DisplayName"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> 
+4
source share
1 answer

I would split it into two TextBlocks and change the visibility only with a trigger. Using Inlines and trying to change Text in a trigger, you are likely to run into priority problems, and Inlines cannot be Inlines on Setter .

eg.

 <StackPanel Grid.Column="2" Grid.Row="0" Orientation="Horizontal"> <TextBlock Text="{Binding HonorificVm.DisplayName}" Margin="0,0,5,0"> <TextBlock.Style> <Style TargetType="TextBlock"> <Style.Triggers> <DataTrigger Binding="{Binding HonorificVm.Honorific}" Value="{x:Static model:Honorific.NullHonorific}"> <Setter Property="Visibility" Value="Collapsed" /> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> <TextBlock Text="{Binding PersonNameVm.DisplayName}" /> </StackPanel> 

An alternative would be MultiBinding instead of Inlines :

 <TextBlock Grid.Column="2" Grid.Row="0"> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Text"> <Setter.Value> <MultiBinding StringFormat="{}{0} {1}"> <Binding Path="HonorificVm.DisplayName" /> <Binding Path="PersonNameVm.DisplayName" /> </MultiBinding> </Setter.Value> </Setter> <Style.Triggers> <DataTrigger Binding="{Binding HonorificVm.Honorific}" Value="{x:Static model:Honorific.NullHonorific}"> <Setter Property="Text" Value="{Binding PersonNameVm.DisplayName}" /> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> 
+7
source

All Articles