2010-10-19 9 views
8

si le asigno un trozo de texto a la propiedad de un ContentContentPresenter, un control TextBlock es generado por el ContentPresenter en tiempo de render para contener ese texto.WPF 4 estilo ContentPresenter TextWrapping no se aplica a implicitedly generada TextBlock

Si creo un estilo que se aplica a las propiedades TextBlock y lo asigno a ese ContentPresenter, no parece aplicarse a las TextBlock s generadas implícitamente.

<Style x:Key="SampleStyle"> 
    <Setter Property="TextBlock.TextWrapping" Value="Wrap"/> 
</Style> 

<ContentPresenter Content="This is a Test piece of text." Style="{StaticResource SampleStyle}"/> 

¿Hay una manera de aplicar este estilo con éxito a la autogenerado TextBlock s corto de aplicarlo a todas las TextBlock s (por ejemplo, declarando estilo que TargetType="TextBlock" sin Key)?

Respuesta

32

Usted puede hacer esto ...

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

... a continuación, donde se define su ContentPresenter ...

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

El TargetType se fija ya que, como se conoce el ContentPresenter no siempre mantenga un TextBlock en él.

+0

Gracias por lo MCH – Sayka

5

Si no está utilizando el estilo otra parte, se podría aplicar directamente a la presentadora de contenido:

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