Ada*_*ynn 10 wpf styles contentpresenter
如果我将一段文本分配给a的Content属性,则由at渲染时生成ContentPresenter一个TextBlock控件ContentPresenter以包含该文本.
如果我创建一个适用于TextBlock属性并将其分配给它的样式,则该样式ContentPresenter似乎不适用于隐式生成的TextBlocks.
<Style x:Key="SampleStyle">
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
</Style>
<ContentPresenter Content="This is a Test piece of text." Style="{StaticResource SampleStyle}"/>
Run Code Online (Sandbox Code Playgroud)
有没有办法成功地将这种风格应用于自动生成TextBlocks将其应用于所有TextBlocks(例如声明样式为TargetType="TextBlock"no Key)?
Aar*_*ver 35
你可以这样做...
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type TextBlock}" x:Key="WrappingStyle">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
...然后你定义你的ContentPresenter...
<ContentPresenter Content="This text is going to wrap...">
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource WrappingStyle}"/>
</ContentPresenter.Resources>
</ContentPresenter>
Run Code Online (Sandbox Code Playgroud)
这TargetType是设置,因为你知道它ContentPresenter不会总是包含TextBlock在其中.
如果您未在其他地方使用该样式,则可以将其直接应用于内容呈现器:
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</ContentPresenter.Resources>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9125 次 |
| 最近记录: |