TextBlock style triggers
Asked Answered
R

1

6

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

I could easily do this in either a converter or a parent view model, but am

This displays nothing at all:

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

Why?

Rail answered 25/6, 2012 at 21:16 Comment(1)
Btw, if this shows nothing at all there probably is something amiss with your DataContext and/or the bindings.Blasted
B
9

I would split it into two TextBlocks and only change the visibility using a trigger. By using the Inlines and trying to change the Text in the triggers you probably run into precedence problems and the Inlines cannot be extracted to a Setter.

e.g.

<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 a 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>
Blasted answered 25/6, 2012 at 21:22 Comment(4)
I am using the inlines to get the Run; wouldn't that be a problem with two text blocks? Can you scratch out some code?Rail
@Berryl: I don't quite get what you mean; added an example.Blasted
What I meant by the Run was that I knew by using it both pieces would display with the proper space between them. How did you know that Margin setting 0,0,5,0 would work so nicely?Rail
@Berryl: I just commonly use that and it seems to fit in most cases, to make it scale properly and be font-dependent you could drop the Margin and add a space to the first TextBlock using StringFormat in the binding ("{}{0} ").Blasted

© 2022 - 2024 — McMap. All rights reserved.