按钮上的个人风格

时间:2013-09-24 12:26:05

标签: c# wpf xaml

我遇到与按钮样式有关的问题。我想在点击按钮后删除蓝色边框。我尝试了在互联网上找到的一些解决方案(herehere),但我找不到任何解决方案。

以下是我要删除的示例:

image

我使用此代码来设置我的按钮样式:

 <Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="AliceBlue"/>
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Margin" Value="10,308,0,0"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Height" Value="60"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    </Style>
</Window.Resources>

3 个答案:

答案 0 :(得分:0)

试试这个

  <Button Content="Text" Name="Button Name" 
                    Background="AliceBlue" 
                    VerticalAlignment="Top" 
                    BorderBrush="Transparent"
                    Margin="10,308,0,0"
                    HorizontalAlignment="Left"
                    Width="200"
                    Height="60"
                    FontSize="20"
                    FocusVisualStyle="{x:Null}">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                 <ContentPresenter Content="{TemplateBinding Content}"/>
            </ControlTemplate>
        </Button.Template>
    </Button>

答案 1 :(得分:0)

将此添加到您的风格中:

BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}

这使得它的行为类似于工具按钮,并且在您点击它之后不会显示边框。

编辑:

我刚刚发现了一个非常简单的controltemplate示例,您可以在按钮类型上应用矩形(wpf magic!)。将StrokeThickness设置为零,不会显示任何边框。

<Window x:Class="WPFControlTemplateSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/exp ression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="289" d:DesignWidth="574">
<Window.Resources>
    <ControlTemplate x:Key="SimpleButton" TargetType="Button">
        <Grid>
            <Rectangle Name="rect" 
                     Width="{TemplateBinding Width}"
                     Height="{TemplateBinding Height}"
                     Stroke="Black"
                     StrokeThickness="0"
                     Fill ="Green"/>
            <ContentPresenter HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Content="{TemplateBinding Content}"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="Button.IsMouseOver" Value="True">
                <Setter TargetName="rect" Property="Stroke" Value="Red"/>
                <Setter TargetName="rect" Property="Fill" Value="Yellow"/>
            </Trigger>
            <Trigger Property="Button.IsPressed" Value="True">
                <Setter TargetName="rect" Property="Stroke" Value="Green"/>
                <Setter TargetName="rect" Property="Fill" Value="Blue"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Button Template="{StaticResource SimpleButton}" Grid.Column="0" Grid.Row="0">Button 1</Button>
    <Button Template="{StaticResource SimpleButton}" Grid.Column="1" Grid.Row="0">Button 2</Button>
</Grid>

答案 2 :(得分:0)

    <Style x:Key="button_style" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                      <ContentPresenter Width="250" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

,例如:

<Button Content = "my_content" Style="{StaticResource button_style}"/>

蓝色边框会消失。