If I assign a piece of text to the Content
property of a ContentPresenter
, a TextBlock
control is generated by the ContentPresenter
at render time to contain that text.
If I create a style that applies toTextBlock
properties and assign it to that ContentPresenter
, the does not appear to apply to the implicitly generated TextBlock
s.
<Style x:Key="SampleStyle">
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
</Style>
<ContentPresenter Content="This is a Test piece of text." Style="{StaticResource SampleStyle}"/>
Is there a way to apply this style successfully to the autogenerated TextBlock
s short of applying it to all TextBlock
s (e.g. declaring style as TargetType="TextBlock"
with no Key
)?
You can do this...
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type TextBlock}" x:Key="WrappingStyle">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</ResourceDictionary>
</Window.Resources>
...then where you define your ContentPresenter
...
<ContentPresenter Content="This text is going to wrap...">
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource WrappingStyle}"/>
</ContentPresenter.Resources>
</ContentPresenter>
The TargetType
is set since as you know the ContentPresenter
will not always hold a TextBlock
in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With