0
    <Style x:Key="MyButton" TargetType="{x:Type Border}">
        <Style.Resources>
            <Style TargetType="{x:Type TextBlock}">                    
                <Setter Property="FontSize" Value="24"/>
                <Setter Property="Foreground" Value="{StaticResource FontColor}"/>                    
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="Cursor" Value="Hand"/>
                <Setter Property="FontWeight" Value="DemiBold" />
                <Setter Property="Effect">
                    <Setter.Value>
                        <BlurEffect Radius="2" ></BlurEffect>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="FontWeight" Value="ExtraBold" />
                    </Trigger>                                                
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>  

我想创建自己的包含边框和文本块的按钮样式。如何为我的样式添加控件?我实际上想添加一个额外的文本块,以便我可以模糊后面的文本框,使其看起来像发光。

4

2 回答 2

2

我不确定您希望您的第二个模糊文本块看起来如何,但下面的 xaml 应该可以让您顺利创建您所追求的按钮模板。

每个文本块以及边框都有自己的样式,因此您可以单独设置它们的样式并将它们全部包装在按钮样式的控件模板中:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="MainTextBlock" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="24"/>
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="FontWeight" Value="DemiBold" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="FontWeight" Value="ExtraBold" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="BlurTextBlock" TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="24"/>
        <Setter Property="Foreground" Value="Blue"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="FontWeight" Value="DemiBold" />
        <Setter Property="Effect">
            <Setter.Value>
                <BlurEffect Radius="2" ></BlurEffect>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="BorderStyle" TargetType="Border">
        <Setter Property="BorderBrush" Value="Orange" />
        <Setter Property="BorderThickness" Value="2" />
    </Style>

    <Style x:Key="MyButton" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Style="{StaticResource BorderStyle}">
                        <Grid>
                            <TextBlock Style="{StaticResource MainTextBlock}" Text="{TemplateBinding Content}" />
                            <TextBlock Style="{StaticResource BlurTextBlock}" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource ResourceKey=MyButton}" Height="30" Width="100" Content="BUTTON" />
</Grid></Window>
于 2012-04-04T03:53:11.173 回答
0

Button 没有 Text 属性,它有 Content 属性。

Content 属性可以是字符串,但您不能期望它始终是字符串。

问题是你想为内容设置样式但你不能。它是一个容器。您可以将图像或网格或整个 xaml 层次结构添加到内容属性。

所以你需要在 Content 中添加一个 TextBlock。

<Button HorizontalAlignment="Left" Margin="190,113,0,0" VerticalAlignment="Top" Width="75">
    <Button.Content>
        <Grid>
            <TextBlock Text="Click Me!" />
            <TextBlock Text="Click Me!">
                <TextBlock.Effect>
                    <BlurEffect Radius="2" />
                </TextBlock.Effect>
            </TextBlock>
        </Grid>
    </Button.Content>
</Button>

所以这似乎不需要样式,它只需要您将属性 Content 添加到 Button.Content 属性。

于 2012-04-04T04:06:28.017 回答