0

I need create many TextBlock with same text.

I can set multiline text in TextBlock like this:

<TextBlock>
    <Run Foreground="Red">the first line</Run><LineBreak />
    the second line
</TextBlock>

I want to create a style for those TextBlock, so I try this:

<Style TargetType="TextBlock" x:Key="text1">
     <Setter Property="Text">
        <Setter.Value>
            <Run Foreground="Red">the first line</Run>
            <LineBreak />
            the second line
        </Setter.Value>
    </Setter>
</Style>

But it's not work.

I think maybe I should add something in <Setter.Value>, I tried a few things but nothing worked, either.

Sorry for bad English and thanks for any idea.

1 Answers1

0

My friend use other key word find this answer: Multiple Run elements in WPF Style setter

It's mean I can't use style set multiple Run elements, so I find other way to solve this problem:

1.Using namespace xmlns:sys="clr-namespace:System;assembly=mscorlib"

2.Add resource

<Window.Resources>
    <sys:String x:Key="myString1">Line1 text</sys:String>
    <sys:String x:Key="myString2">Line2 text</sys:String>
</Window.Resources>

3.Use resource

<TextBlock>
    <Run Foreground="Red" Text="{StaticResource myString1}"/>
    <LineBreak />
    <Run Text="{StaticResource myString2}"/>
</TextBlock>