我有一个自定义的WPF控件。它有一个嵌套的ContentControl,它绑定到模板的Content属性,因此它可以将任何对象设置为其内容。
如果内容是原始字符串,则我想将以下样式应用于TextBlock(当实际渲染可视树时,如果将ContentControl的Content属性设置为字符串,则会生成一个包含TextBlock的ContentPresenter)。
如果内容是原始字符串,则我想将以下样式应用于TextBlock(当实际渲染可视树时,如果将ContentControl的Content属性设置为字符串,则会生成一个包含TextBlock的ContentPresenter)。
<Style x:Key="Label" TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush>
<SolidColorBrush.Color>
<Color A="255" R="82" G="105" B="146" />
</SolidColorBrush.Color>
</SolidColorBrush>
</Setter.Value>
</Setter>
</Style>
我认为实现这个的方法是通过嵌套资源(这是我的自定义控件的一部分):
<ContentControl x:Name="SomeText" Margin="10,10,10,0"
Content="{TemplateBinding Content}"
IsTabStop="False" Grid.Column="1">
<ContentControl.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource Label}" />
</ContentControl.Resources>
</ContentControl>
所以...对我来说,上面的内容表明如果ContentControl最终带有嵌套的TextBlock,则应该应用Label样式,是吗?但在上面的示例中未应用Label样式。
我该如何达到这个效果呢?
谢谢。