当 Content 是字符串时,在 Silverlight 中为 ContentPresenter 创建的隐式 Textblock 下划线?
我正在尝试为内容控件(例如 Button 或 HeaderedContentControl 等)创建一个模板,其中文本带有下划线。
我只想在指定 Content="This text is underlined"
时为文本添加下划线。
如果 Content 是另一个 UIElement,它必须继续正常工作。
大多数提出同样问题的帖子都满足于将模板修改为仅适用于字符串作为内容。 Scott Gu 有一篇关于 样式按钮,但没有解决此问题。
如果您实际将 Content
作为 TextBlock
类型的实例而不是字符串传递,则以下示例将起作用。当然,视觉树有一个 TextBlock,所以它应该设置它的样式。也许这是 Sivlerlight 的限制。
当我希望它显示为大红色文本时,此示例显示黑色文本和大红色文本。
<navigation:Page.Resources>
<Style TargetType="TextBlock" x:Key="style123">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="72"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="TextDecorations" Value="Underline"/>
</Style>
</navigation:Page.Resources>
<StackPanel>
<!-- This doesn't work and shows black text -->
<ContentPresenter Content="Small black text">
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource style123}"/>
</ContentPresenter.Resources>
</ContentPresenter>
<!-- This works and shows red text -->
<ContentPresenter>
<ContentPresenter.Content>
<TextBlock Text="This is big red text"/>
</ContentPresenter.Content>
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource style123}"/>
</ContentPresenter.Resources>
</ContentPresenter>
</StackPanel>
I am trying to create a template for a content control such as Button or HeaderedContentControl etc. where the text is underlined.
I just want to underline the text when Content="This text is underlined"
is specified.
It must continue to work as normal if Content is another UIElement.
Most posts asking this same question are satisfied with modifying the template to only work for a string as content. Scott Gu has a good article about styling buttons but doesn't address this issue.
The following sample will work if you actually pass in Content
as an instance of type TextBlock
but not as a string. Surely the visual tree has a TextBlock so it should style it. Perhaps this is a Sivlerlight limitation.
This example shows black text and big red text when I want it to display both as big red text.
<navigation:Page.Resources>
<Style TargetType="TextBlock" x:Key="style123">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="72"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="TextDecorations" Value="Underline"/>
</Style>
</navigation:Page.Resources>
<StackPanel>
<!-- This doesn't work and shows black text -->
<ContentPresenter Content="Small black text">
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource style123}"/>
</ContentPresenter.Resources>
</ContentPresenter>
<!-- This works and shows red text -->
<ContentPresenter>
<ContentPresenter.Content>
<TextBlock Text="This is big red text"/>
</ContentPresenter.Content>
<ContentPresenter.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource style123}"/>
</ContentPresenter.Resources>
</ContentPresenter>
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以对您正在使用的任何实际
ContentControl
(即Button
)进行子类化并覆盖OnContentChanged
以重置Content
如果 newContent 是字符串,则属性为带下划线的TextBlock
。如果 newContent 不是字符串,它将以通常的方式执行。为了实现这一点而进行子类化有点烦人,但它避免了混乱的样式模板和子类化
ContentPresenter
。You could subclass whatever actual
ContentControl
(i.e.Button
) you are using and overrideOnContentChanged
in order to reset theContent
property to an underlinedTextBlock
if the newContent is a string. In the case that the newContent is not a string it would perform in the usual way.It's kind of annoying to subclass just to accomplish this but it avoids messy style templates and subclassing
ContentPresenter
.尝试这个示例,使用 DataTemplate 自定义渲染
string
内容(我刚刚将背景设置为红色):编辑:就像注释一样,您可以将其拉出到
ContentControl 中
样式而不是每次都内联应用它,如果您需要更好的可重用性......Try this example, using a DataTemplate to custom-render
string
content (I've just set the background to red):EDIT: just as a note, you could pull this out into a
ContentControl
style rather than applying it inline each time, if you need better reusability...