Jim
Jim

Reputation: 2828

GridViewColumn CellTemplate Conditional Visibility of Text Run

In below snippet I would like to hide the Run Text when the second property Prop2 is null or empty. What is the best way of doing this?

<GridViewColumn>
  <GridViewColumn.CellTemplate>
    <DataTemplate>
        <TextBlock >
            <Run Text="{Binding Prop1}"></Run>
            <Run Text=" {"></Run> <----- Collapse/Hide when Prop2 is null or empty
            <Run Text="{Binding Prop2}"></Run><----- Collapse/Hide when Prop2 is null or empty
            <Run Text="}"></Run><----- Collapse/Hide when Prop2 is null or empty
        </TextBlock>
    </DataTemplate>
</GridViewColumn.CellTemplate>

Upvotes: 0

Views: 70

Answers (1)

Jesse Good
Jesse Good

Reputation: 52365

You can take advantage of StringFormat and TargetNullValue.

<Run Text="{Binding Prop1}"></Run>
<Run Text="{Binding Prop2, StringFormat={}{{{0}}}, TargetNullValue={}}"></Run>

Upvotes: 1

Related Questions