14

Is it possible in XAML to define multiple Run's inside a Style setter?

The following has two Run's defined and fails:

The property 'Value' is set more than once.

<TextBlock>
    <TextBlock.Style>
         <Style TargetType="{x:Type TextBlock}">
              <Setter Property="Text">
                   <Setter.Value>
                       <Run Text="{Binding SelectedItem.iso}"/>
                       <Run Text="{Binding SelectedItem.value}"/>
                  </Setter.Value>
              </Setter>
             <Style.Triggers>
                 <DataTrigger Binding="{Binding SelectedItem.type}" Value={x:Null}">
                      <Setter Property="Text" Value="No value" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Can this be fixed while preserving the usage of multiple Run's?

Nuts
  • 2,755
  • 6
  • 33
  • 78
  • This would not work even if a Style Setter would accept multiple values. Multiple Runs are not set to the `Text` property of a TextBlock, but instead to its `Inlines` property. However, `Inlines` is not a dependency property and can therefore not be set by a Style Setter. – Clemens Jan 28 '15 at 12:41
  • Check out this: http://stackoverflow.com/questions/11197474/textblock-style-triggers It has the two options available for you. – Amit Raz Jan 28 '15 at 12:47

1 Answers1

15

A Setter works on one property, so it can have only one value, the error you get is logical: it has no way of understanding what you're trying to do, it can just... set a property to a given value.

So the idea is to give it this value as it should be: appended texts. To do so, you would use MultiBinding, which takes multiple values and returns them as one, depending on the StringFormat you give it:

<Setter.Value>
    <MultiBinding StringFormat="{}{0}{1}{2}"><!-- Format as you wish -->
        <Binding Path="SelectedItem.iso"/>
        <Binding Source="{x:Static System:Environment.NewLine}"/>
        <Binding Path="SelectedItem.value"/>
    </MultiBinding>
</Setter.Value>

Note on StringFormat: You have to use {} at start to escape braces, else it would consider them as markup extension starters.

Kilazur
  • 3,089
  • 1
  • 22
  • 48