如何设置WPF按钮控件的文本样式?

时间:2014-12-23 20:31:27

标签: wpf xaml resourcedictionary

使用过FXML,看到了FXML CSS样式和XAML ResourceDictionary之间的相似之处。

使用FXML我可以设置按钮的标签样式,使其具有内阴影效果。

我正在尝试使用XAML中的按钮。

我不需要确切的代码来实现样式,我只需要知道代码在资源字典中的标记位置。

如何设置XAML按钮文本的样式?

修改

我要感谢McGarnagle让我朝着正确的方向前进: 这是我能够提出的(感谢我能够从here获得)

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Grid.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </Grid.Resources>
                    <TextBlock Foreground="{DynamicResource{x:Static SystemColors.GrayTextBrushKey}}" Text="{TemplateBinding Content}">
                                <TextBlock.RenderTransform>
                                    <TranslateTransform X="-2" Y="-2" />
                                </TextBlock.RenderTransform>
                    </TextBlock>
                    <TextBlock Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

3 个答案:

答案 0 :(得分:1)

您可以使用Effect属性向任何元素添加效果并添加DropShadowEffect。如果只想模糊按钮内的文本,则必须覆盖控件模板。这是一个简单的例子:

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <TextBlock Text="{TemplateBinding Content}">
                        <TextBlock.Effect>
                            <DropShadowEffect BlurRadius="2" />
                        </TextBlock.Effect>
                    <TextBlock>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

要使用投影重新创建默认按钮样式,只需复制粘贴default control template并将阴影效果添加到内容元素即可。为此,您必须将ContentPresenter元素替换为ContentControl

<ContentControl Margin="2"
                Content="{TemplateBinding Content}"
                ContentTemplate="{TemplateBinding ContentTemplate}"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                >
    <ContentControl.Effect>
        <DropShadowEffect BlurRadius="3" />
    </ContentControl.Effect>
</ContentControl>

drop-shadow button

答案 1 :(得分:0)

Page而不是按钮,但我认为这就是你所要求的。

<Page.Resources>
    <Style x:Key="GridHeaderStyleRight" TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Right"/>
        <Setter Property="Background" Value="LightGray" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="Margin"  Value="4,4,4,4" />
    </Style>

答案 2 :(得分:0)

这将自定义当前应用中使用的所有按钮的默认按钮样式:

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="MyButtonStyle" TargetType="Button">
            <!-- Change whatever you want. -->
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="FontSize" Value="15" />
            <Setter Property="Template">
                <Setter.Value>
                     <!-- Custom Template -->
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Application.Resources>