4

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

<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 的情况下做到这一点?

谢谢

4

1 回答 1

0

你绝对可以,或者我不明白这个问题,但看看这个例子是否是你所追求的:

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

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

于 2010-08-18T17:10:16.413 回答