13

テキストの一部をのContentプロパティに割り当てると、レンダリング時にそのテキストを含むContentPresenterコントロールTextBlockが生成されます。ContentPresenter

プロパティに適用するスタイルを作成してTextBlockそれに割り当てるとContentPresenter、暗黙的に生成されたに適用されているようには見えませんTextBlock

<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に適用する以外に、自動生成されたsに正常に適用する方法はありますか(たとえば、スタイルをnoのようにTextBlock宣言する)?TargetType="TextBlock"Key

4

2 に答える 2

39

あなたはこれを行うことができます...

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

...次に、あなたがあなたを定義する場所ContentPresenter...

<ContentPresenter Content="This text is going to wrap...">
            <ContentPresenter.Resources>
                <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource WrappingStyle}"/>
            </ContentPresenter.Resources>
</ContentPresenter>

TargetTypeご存知のように、が常に保持されるとは限らないため、が設定されContentPresenterますTextBlock

于 2010-10-19T16:23:43.177 に答える
9

他の場所でスタイルを使用していない場合は、コンテンツプレゼンターに直接適用できます。

<ContentPresenter.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
</ContentPresenter.Resources>
于 2014-07-21T09:31:16.327 に答える