WPF 自定义简单的Button 按钮

前言:想实现自己项目中的Button 按钮UI,后续根据需求再添加附加属性或带Icon的按钮

因为之前有做Web项目所以前后端都得搞搞,现在搞到WPF感觉界面不能太单调了

设计前可参考element-ui 一些成熟的UI 框架

 

目前仅实现比较简单的样式:

1、创建cs文件继承Button 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace YOI.Controls
{

public class UButton : Button
{

static UButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(UButton), new FrameworkPropertyMetadata(typeof(UButton)));
}


}
}

2、创建xaml 资源样式文件

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:YOI.Controls">

  <Style TargetType="{x:Type local:UButton}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type local:UButton}">
          <!--  定义视觉树  -->
          <Border
            x:Name="btnBorder"
            Width="{TemplateBinding Width}"
            Height="{TemplateBinding Height}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            CornerRadius="0"
            SnapsToDevicePixels="True">
            <TextBlock
              x:Name="contentPresenter"
              Padding="{TemplateBinding Padding}"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              FocusVisualStyle="{x:Null}"
              FontFamily="{TemplateBinding FontFamily}"
              Text="{TemplateBinding Content}" />
          </Border>

          <!--  定义视觉树  -->
          <ControlTemplate.Triggers>

            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="IsPressed" Value="false" />
              </MultiTrigger.Conditions>
              <MultiTrigger.Setters>
                <!--<Setter Property="Effect">
                <Setter.Value>
                <DropShadowEffect
                BlurRadius="3"
                ShadowDepth="1"
                Color="#DDDDDD" />
              </Setter.Value>
              </Setter>-->
                <Setter TargetName="btnBorder" Property="Background" Value="{DynamicResource PrimaryBlueColor}" />
                <Setter TargetName="btnBorder" Property="BorderThickness" Value="0" />
                <Setter TargetName="btnBorder" Property="Opacity" Value="0.75" />
                <Setter TargetName="contentPresenter" Property="Foreground" Value="{DynamicResource PrimaryGreen}" />
              </MultiTrigger.Setters>

            </MultiTrigger>


            <Trigger Property="IsPressed" Value="True">
              <Setter TargetName="btnBorder" Property="Background" Value="{DynamicResource PrimaryBlueColor}" />
              <Setter TargetName="btnBorder" Property="BorderThickness" Value="0" />
              <Setter TargetName="contentPresenter" Property="FontWeight" Value="Bold" />
              <Setter TargetName="contentPresenter" Property="Foreground" Value="{DynamicResource PrimaryGreen}" />
            </Trigger>

            </ControlTemplate.Triggers>

            </ControlTemplate>

            </Setter.Value>
            </Setter>
            </Style>

            </ResourceDictionary>

3、 Generic.xaml 引用

 

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">


    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MotionControls;Component/Themes/Button/UButton.xaml" />
        <ResourceDictionary Source="/MotionControls;Component/Themes/GroupBox/UGroupBox.xaml" />

        <ResourceDictionary Source="/MotionControls;Component/Themes/SwitchButton/USwitchButton.xaml" />

        <ResourceDictionary Source="/MotionControls;Component/Themes/TextBox/UTextBox.xaml" />

        <ResourceDictionary Source="/MotionControls;Component/Themes/TabControl/UTabControl.xaml" />
        <ResourceDictionary Source="/MotionControls;Component/Themes/TabItem/UTabItem.xaml" />

        <ResourceDictionary Source="/MotionControls;Component/Themes/Colors.xaml" />

    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

4、调用

<YOI:UButton
  Width="70"
  Height="30"
  Margin="10"
  Background="{DynamicResource TextWhiteColor}"
  BorderBrush="{DynamicResource PrimaryBlueColor}"
  BorderThickness="1"
  Content="{Binding Content}"
  Cursor="Hand"
  Foreground="{DynamicResource Tex
  </YOI:UButton>

总结:整体就就实现了自定义控件简单Button 按钮

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Blend中创建WPF自定义Button控件,可以按照以下步骤进行: 1. 打开Blend,创建一个新的WPF项目。 2. 在“项目”面板中,右键单击“控件”文件夹,选择“添加”->“新建项”。 3. 在“添加新项”对话框中,选择“WPF”->“Custom Control”,设置名称为“CustomButton”并选择位置,点击“添加”按钮。 4. Blend会自动生成一个名为“CustomButton”的自定义控件的类文件和一个默认的控件模板文件。 5. 双击控件模板文件,进入“编辑模板”模式。在这里,你可以自由地编辑控件的外观和布局。 6. 在“对象和时间”面板中,可以选择控件的外观和行为。例如,你可以添加按钮、文本框等控件,设置它们的属性和事件处理程序。 7. 在控件模板中,找到名为“PART_Button”控件的模板,这是自定义控件中的按钮。你可以编辑它的外观和行为,以实现自定义Button控件的功能和样式。 8. 在编辑完成后,保存模板文件并退出“编辑模板”模式。 9. 在CustomButton类中,添加自定义属性和事件处理程序,以实现自定义Button控件的功能。 10. 在应用程序中,使用自定义Button控件,只需要在XAML中添加一个CustomButton标记,然后设置它的属性和事件处理程序即可。 以上就是在Blend中创建WPF自定义Button控件的基本步骤。需要注意的是,在创建控件时,应该考虑控件的可重用性和灵活性,以便在不同的场景中使用。同时,应该设计好控件的外观和行为,以便用户可以方便地使用和定制控件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值