是否可以在 Silverlight ContentPresenter 中嵌入文本格式?

发布于 2024-08-05 23:53:54 字数 891 浏览 6 评论 0原文

我刚刚开始使用样式和控件模板,并创建了以下样式以将按钮显示为网格中的文本。我想将字体样式嵌入到下划线样式中,但还没有弄清楚。

<Style x:Key="TextButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="LightGray" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <ContentPresenter Content="{TemplateBinding Content}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="TextButtonInGridStyle" TargetType="Button" BasedOn="{StaticResource TextButtonStyle}">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Margin" Value="4,4,4,4" />
</Style>

我想将字体样式嵌入到下划线样式中,但还没有弄清楚。有没有办法在不将 TextBlock 嵌入 ControlTemplate 或在 Button 元素声明中嵌套 TextBlock 的情况下执行此操作?

谢谢

I have just started working with with Styles and Control Templates and have created the following styles to display a button as text in a grid. I would like to embed Font Styling into the styles for underlining but have not figured it out.

<Style x:Key="TextButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="LightGray" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <ContentPresenter Content="{TemplateBinding Content}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="TextButtonInGridStyle" TargetType="Button" BasedOn="{StaticResource TextButtonStyle}">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Margin" Value="4,4,4,4" />
</Style>

I would like to embed Font Styling into the styles for underlining but have not figured it out. Is there any way to do this without embedding a TextBlock into the ControlTemplate or nesting a TextBlock in the Button element declaration?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小兔几 2024-08-12 23:53:54

你绝对可以,或者也许我不明白这个问题,但看看这个例子是否是你想要的:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="SilverlightApplication1.MainPage"
Width="640" Height="480">
<UserControl.Resources>
    <Style x:Key="TextButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="LightGray" />
        <Setter Property="Template">
            <Setter.Value>
                    <ControlTemplate TargetType="ContentControl">
                            <ContentPresenter Content="{TemplateBinding Content}" />
                    </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="TextButtonInGridStyle" TargetType="Button" BasedOn="{StaticResource TextButtonStyle}">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="4,4,4,4" />
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="Red"/>
    </Style>
</UserControl.Resources>

<StackPanel x:Name="LayoutRoot" Background="White">
    <Button Content="Button" Style="{StaticResource TextButtonStyle}"/>
    <Button HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="75" Style="{StaticResource TextButtonInGridStyle}" Margin="4,0,0,0">
        Button
    </Button>
    <Button HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="75" Style="{StaticResource TextButtonInGridStyle}" Margin="4,0,0,0">
        <Rectangle Fill="#FFF4F4F5" Height="10" Stroke="Black"/>
    </Button>
</StackPanel>

您可以看到按钮中没有 TextBlock,如果内容是矩形,它将很好地显示而不是文本。

You definitely can, or maybe I don't understand the problem, but see if this example is what you're after:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="SilverlightApplication1.MainPage"
Width="640" Height="480">
<UserControl.Resources>
    <Style x:Key="TextButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="LightGray" />
        <Setter Property="Template">
            <Setter.Value>
                    <ControlTemplate TargetType="ContentControl">
                            <ContentPresenter Content="{TemplateBinding Content}" />
                    </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="TextButtonInGridStyle" TargetType="Button" BasedOn="{StaticResource TextButtonStyle}">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Margin" Value="4,4,4,4" />
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="Red"/>
    </Style>
</UserControl.Resources>

<StackPanel x:Name="LayoutRoot" Background="White">
    <Button Content="Button" Style="{StaticResource TextButtonStyle}"/>
    <Button HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="75" Style="{StaticResource TextButtonInGridStyle}" Margin="4,0,0,0">
        Button
    </Button>
    <Button HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="75" Style="{StaticResource TextButtonInGridStyle}" Margin="4,0,0,0">
        <Rectangle Fill="#FFF4F4F5" Height="10" Stroke="Black"/>
    </Button>
</StackPanel>

You can see that there's no TextBlock in the Button, and if the content is a rectangle it'll be shown instead of the text nicely.

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