WPF 4 ContentPresenter TextWrapping样式未应用于隐式生成的TextBlock

WPF 4 ContentPresenter TextWrapping样式未应用于隐式生成的TextBlock,wpf,styles,contentpresenter,Wpf,Styles,Contentpresenter,如果我将一段文本指定给ContentPresenter的Content属性,则ContentPresenter会在渲染时生成一个TextBlock控件来包含该文本 如果我创建了一个应用于TextBlock属性的样式,并将其分配给该ContentPresenter,则不会应用于隐式生成的TextBlocks <Style x:Key="SampleStyle"> <Setter Property="TextBlock.TextWrapping" Value="Wrap"/&g

如果我将一段文本指定给
ContentPresenter
Content
属性,则
ContentPresenter
会在渲染时生成一个
TextBlock
控件来包含该文本

如果我创建了一个应用于
TextBlock
属性的样式,并将其分配给该
ContentPresenter
,则不会应用于隐式生成的
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}"/>

除了将此样式应用于所有
TextBlock
s(例如,将样式声明为
TargetType=“TextBlock”
,而不使用
键)之外,是否有其他方法可以将此样式成功应用于自动生成的
TextBlock

<Window.Resources>
    <ResourceDictionary>
        <Style TargetType="{x:Type TextBlock}" x:Key="WrappingStyle">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
    </ResourceDictionary>
</Window.Resources>

设置了
TargetType
,因为您知道
ContentPresenter
不会始终在其中包含
TextBlock

如果您没有在其他地方使用该样式,可以将其直接应用于内容presenter:

<ContentPresenter.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
</ContentPresenter.Resources>

非常好的解决方案,这允许默认行为,即仅从字符串或字符串资源自动创建文本框,并预定义文本框的自定义样式,或者如果需要多个简单文本框,则允许定义自定义内容。
<ContentPresenter.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
</ContentPresenter.Resources>