Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Run elements in WPF Style setter

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?

like image 930
Nuts Avatar asked Jan 28 '15 12:01

Nuts


1 Answers

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.

like image 108
Kilazur Avatar answered Sep 21 '22 18:09

Kilazur