WPF 无边框button

项目中,有时会需要无边框的Button,如果没有特别的其他功能需要,我们可以更简单的实现这一点:

<Button Content="myButton" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>

当然,上面的按钮类似于ToolBar风格,移动到上面是蓝色背景。

如果想进一步简单修改,可以用以下方法,实现移动到按钮上出现阴影,按下时内容缩小的功能:

 <Button Command="{Binding RefreshSQLCmd}" ToolTip="{DynamicResource Refresh}" SnapsToDevicePixels="True" Background="Transparent" Height="30" Width="30" BorderBrush="Transparent">
                        <Button.Template>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Grid>
                                    <Rectangle Height="30" Width="30">
                                        <Rectangle.Fill>
                                            <ImageBrush ImageSource="Images/refresh.ico"></ImageBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"></ContentPresenter>
                                </Grid>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsMouseOver" Value="true">
                                        <Setter Property="Image.Effect">
                                            <Setter.Value>
                                                <DropShadowEffect BlurRadius="20" ShadowDepth="2" Color="#FF446FCB" Opacity="0.8"/>
                                            </Setter.Value>
                                        </Setter>
                                    </Trigger>
                                    <Trigger Property="IsPressed" Value="True">
                                        <Setter Property="RenderTransform">
                                            <Setter.Value>
                                                <ScaleTransform ScaleX="0.95" ScaleY="0.95"/>
                                            </Setter.Value>
                                        </Setter>
                                        <Setter Property="RenderTransformOrigin" Value=".5,.5"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Button.Template>  
                    </Button>

使用图片作为按钮外观时,如果需要IsEnable为false时变灰,可以通过自定义如下:

  <Style x:Key="myButton1" TargetType="Button">
        <Style.Resources>
            <Style TargetType="Image">
                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" Value="0.5" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>

如果想更复杂的,如下面的圆形按钮,界面上的Button只需要Style绑定就可以了:

    <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Foreground" Value="Black"/>
        <!--修改模板属性-->
        <Setter Property="Template">
            <Setter.Value>
                <!--控件模板-->
                <ControlTemplate TargetType="Button">
                    <!--背景色-->
                    <Border x:Name="back" Opacity="0.8" CornerRadius="3">
                        <Border.BitmapEffect>
                            <!--边框为0的外发光效果-->
                            <OuterGlowBitmapEffect Opacity="0.7" GlowSize="0" GlowColor="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" />
                        </Border.BitmapEffect>
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1.5">
                                <GradientBrush.GradientStops>
                                    <GradientStopCollection>
                                        <GradientStop Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" Offset="0"/>
                                        <GradientStop Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(Button.Background).(SolidColorBrush.Color)}" Offset="0.4"/>
                                        <GradientStop Color="#FFF" Offset="1"/>
                                    </GradientStopCollection>
                                </GradientBrush.GradientStops>
                            </LinearGradientBrush>
                        </Border.Background>
                        <!--前景色及边框-->
                        <Border x:Name="fore" BorderThickness="1" CornerRadius="3" BorderBrush="#5555">
                            <!--fore 实现按钮的边框和高亮反光效果,半透明的黑色1像素边框,使边框的色彩可以和背景色混合起来-->
                            <!--背景同样采用的渐变笔刷,起始值和终止值的位置几乎贴在一起,从而形成比较鲜明的反光度对比。-->
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientBrush.GradientStops>
                                        <GradientStopCollection>
                                            <GradientStop Color="#6FFF" Offset="0.5"/>
                                            <GradientStop Color="#1111" Offset="0.51"/>
                                        </GradientStopCollection>
                                    </GradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Border.Background>
                            <!--按钮内容-->
                            <ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{TemplateBinding  Content}">
                                <!--一个不太明显的阴影滤镜以增强显示效果-->
                                <ContentPresenter.BitmapEffect>
                                    <DropShadowBitmapEffect Color="#000" Direction="-90" ShadowDepth="2" Softness="0.1" Opacity="0.3" />
                                </ContentPresenter.BitmapEffect>
                            </ContentPresenter>
                        </Border>
                    </Border>
                    <!--触发器-->
                    <ControlTemplate.Triggers>
                        <!--鼠标移入移出-->
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation To="6" Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <ColorAnimation To="#AFFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation To="#3FFF" BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation Duration="0:0:0.2" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                        <!--按钮按下弹起-->
                        <Trigger Property="IsPressed" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation To="3" Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <ColorAnimation To="#3AAA" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation To="#2111" Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                        <!--按钮失效-->
                        <!--当按钮失效时,要改变很多东西,首先将文字颜色设为灰色,然后依次创建了改变外发光效果大小、改变内容阴影效果不透明度、
                        改变内容阴影效果角度、改变内容阴影效果颜色、改变按钮边框颜色、改变上部反光区域颜色、改变下部反光区域颜色的动画。
                         这里将先前对内容应用的阴影效果彻底改变,使之产生凹陷的效果-->
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="#B444"/>
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation To="0" Duration="0:0:0.3" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <DoubleAnimation To="1" Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Opacity)" />
                                        <DoubleAnimation To="-135" Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Direction)" />
                                        <ColorAnimation To="#FFF" Duration="0:0:0.3" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Color)" />
                                        <ColorAnimation To="#D555" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" />
                                        <ColorAnimation To="#CEEE" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation To="#CDDD" Duration="0:0:0.3" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="back" Storyboard.TargetProperty="(Border.BitmapEffect).(OuterGlowBitmapEffect.GlowSize)" />
                                        <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Opacity)" />
                                        <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Direction)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="content" Storyboard.TargetProperty="(ContentPresenter.BitmapEffect).(DropShadowBitmapEffect.Color)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" />
                                        <ColorAnimation Duration="0:0:0.1" Storyboard.TargetName="fore" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

以上是整理了一下自己常用的方法和网络上的一个实例,对自定义控件有一个参考。

时间: 2024-08-26 10:25:28

WPF 无边框button的相关文章

WPF 无边框窗体

第一步:去掉那些最大化最小化和关闭 代码如下: WindowStyle="None" 第二步:去掉那边框 代码如下: AllowsTransparency="True" 第三步:拖动窗体 方法:给窗体设置MouseLeftButtonDown事件 代码如下: (1)前台代码: MouseLeftButtonDown="Border_MouseLeftButtonDown_1" (2)后台代码: private void Border_MouseL

WPF无边框可拖动窗体

下面主要记录下创建无边框窗体,并且可以拖动.这种窗体主要用于弹出小窗体时. <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http

利用WPF创建含多种交互特性的无边框窗体

咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了自行美化窗口通常都会想到使用无边框窗口吧: 可正常最大化,WPF无边框窗口直接最大化会直接使窗口全屏即会将任务栏一并盖住: 窗口边缘改变窗口大小,可以拖动窗口边缘改变大小: 支持等同于标题栏的全窗口空白区拖动,这一特性可以参考QQ: 支持多显示器环境. 上述针对无边框窗口的特性均可以独立实现,本文将

01.WPF中制作无边框窗体

[引用:]http://blog.csdn.net/johnsuna/article/details/1893319 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用GDI+自定义绘制. 那么,在WPF中,我们怎样制作一个无边框窗体呢? 答案是将Window的WindowStyle属性设置为None,即WindowStyle="None" .如果是非矩形的异形窗体,则

[01]可拖动、可调节大小、自定义按钮风格的无边框WPF程序

初学WPF,用VS2012写了一个可拖动.可调节大小.自定义按钮风格的无边框程序.效果如下: 实现过程:一.拖动.调节大小.无边框主要参考了: http://blog.csdn.net/dlangu0393/article/details/12548731 二.自己主要完成自定义按钮效果. 1.  WPF定义按钮风格方法 (1) 添加一个Resource Dictionary文件,如Style.xaml.例如: <ResourceDictionary xmlns="http://schem

使用WPF创建无边框窗体

一.无边框窗口添加窗口阴影 实际上在WPF中添加无边框窗口的窗口阴影十分简单. 首先,设置WindowStyle="None"以及AllowsTransparency="True"使得窗口无边框.并对Window添加DropShadowEffect效果并设定相关参数,在这里我根据设计师的要求设置ShadowDepth="1" BlurRadius="6" Direction="270" Opacity=&q

WPF一步步实现完全无边框自定义Window(附源码)

原文:WPF一步步实现完全无边框自定义Window(附源码) 在我们设计一个软件的时候,有很多时候我们需要按照美工的设计来重新设计整个版面,这当然包括主窗体,因为WPF为我们提供了强大的模板的特性,这就为我们自定义各种空间提供了可能性,这篇博客主要用来介绍如何自定义自己的Window,在介绍整个写作思路之前,我们来看看最终的效果. 图一 自定义窗体主界面 这里面的核心就是重写Window的Template,针对整个开发过程中出现的问题我们再来一步步去剖析,首先要看看我们定义好的样式 <Resou

WPF 窗口去除顶部边框(正宗无边框)

原文:WPF 窗口去除顶部边框(正宗无边框) 最近在做一个大屏展示视频图片的项目,功能并不复杂,半天的工作量吧,一开始同事采用的Unity3D进行开发,但是里面要播放4K视频,Unity 的短板就是视频的播放了,今晚就要交付了,我一早就来公司,决定用WPF重新开发一版,各项功能都好了,唯独顶部总是显示一条白色的边,已经设置WindowStyle为None了也没用,幸得网上大神提供的资料,终于解决了这个小问题. XAML内容如下: <Window x:Class="WPF_VideoPlay

C# WPF 设置窗口无边框

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" AllowsTransparency="True" WindowStyle="None" > AllowsTransparency="True&quo