0

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

这是我要删除的示例:

图片

我用这段代码来设计我的按钮:

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

3 回答 3

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>
于 2013-09-24T12:32:23.920 回答
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}"/>

蓝色边框将消失。

于 2013-09-24T12:50:52.723 回答
0

将此添加到您的样式中:

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

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

编辑:

我刚刚找到了一个非常简单的 controltemplate 示例,您在按钮类型上应用了一个矩形(wpf 魔术!)。将 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>

于 2013-09-24T12:38:20.650 回答