当 Content 是字符串时,在 Silverlight 中为 ContentPresenter 创建的隐式 Textblock 下划线?

发布于 2024-09-13 13:57:26 字数 1831 浏览 8 评论 0原文

我正在尝试为内容控件(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

城歌 2024-09-20 13:57:26

您可以对您正在使用的任何实际 ContentControl(即 Button)进行子类化并覆盖 OnContentChanged 以重置 Content如果 newContent 是字符串,则属性为带下划线的 TextBlock。如果 newContent 不是字符串,它将以通常的方式执行。

public class UnderlineButton : Button
{
    protected override void OnContentChanged(object oldContent, object newContent)
    {
        if (newContent is string)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = newContent as string;
            textBlock.TextDecorations = TextDecorations.Underline;
            this.Content = textBlock;
        }

        base.OnContentChanged(oldContent, newContent);
    }
}

为了实现这一点而进行子类化有点烦人,但它避免了混乱的样式模板和子类化 ContentPresenter

You could subclass whatever actual ContentControl (i.e. Button) you are using and override OnContentChanged in order to reset the Content property to an underlined TextBlock if the newContent is a string. In the case that the newContent is not a string it would perform in the usual way.

public class UnderlineButton : Button
{
    protected override void OnContentChanged(object oldContent, object newContent)
    {
        if (newContent is string)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = newContent as string;
            textBlock.TextDecorations = TextDecorations.Underline;
            this.Content = textBlock;
        }

        base.OnContentChanged(oldContent, newContent);
    }
}

It's kind of annoying to subclass just to accomplish this but it avoids messy style templates and subclassing ContentPresenter.

浅忆流年 2024-09-20 13:57:26

尝试这个示例,使用 DataTemplate 自定义渲染 string 内容(我刚刚将背景设置为红色):

<ContentControl Content="{Binding YourData}" >
  <ContentControl.Resources>
    <DataTemplate DataType="{x:Type s:String}">
      <TextBlock Text="{Binding}" Background="Red" />
    </DataTemplate>
  </ContentControl.Resources>
</ContentControl>

编辑:就像注释一样,您可以将其拉出到 ContentControl 中 样式而不是每次都内联应用它,如果您需要更好的可重用性......

Try this example, using a DataTemplate to custom-render string content (I've just set the background to red):

<ContentControl Content="{Binding YourData}" >
  <ContentControl.Resources>
    <DataTemplate DataType="{x:Type s:String}">
      <TextBlock Text="{Binding}" Background="Red" />
    </DataTemplate>
  </ContentControl.Resources>
</ContentControl>

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文