珂珂的个人博客 - 一个程序猿的个人网站

FYJ.Winui系列(二)-按钮控件

FYJ.Winui系列(一)

FYJ.Winui系列(二)-按钮控件

FYJ.Winui系列(三)-换肤控件

FYJ.Winui系列(四)-基窗体

FYJ.Winui系列(五)-主窗体

   这里做简单了,只写了两种状态,正常状态和鼠标移进去的状态。来看看xaml代码

 <Button x:Class="FYJ.Winui.ButtonUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="21" d:DesignWidth="69"  Name="UC">
    <Button.Template>
        <ControlTemplate>
            <Grid>
                <Image Name="normalImage" Source="{Binding ElementName=UC,Path=ImgSource}" Visibility="Visible"></Image>
                <Image Name="mouseOverImage" Source="{Binding ElementName=UC,Path=MouseEnterImgSource}" Visibility="Collapsed"></Image>
                <Image Name="mouseDownImage" Source="Resources\btn_close_down.png" Visibility="Collapsed"></Image>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="normalImage" Property="Visibility" Value="Collapsed"></Setter>
                    <Setter TargetName="mouseOverImage" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="mouseDownImage" Property="Visibility" Value="Collapsed"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

再是后台代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
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 FYJ.Winui
{
    ///     /// ButtonUserControl.xaml 的交互逻辑
    ///     public partial class ButtonUserControl : Button, INotifyPropertyChanged
    {
        public ButtonUserControl()
        {
            InitializeComponent();
        }

        //不使用绑定 依赖属性没有意义
        public static readonly DependencyProperty ImgSourceProperty = DependencyProperty.Register("UCImgSource", typeof(ImageSource), typeof(ButtonUserControl), null);
        public static readonly DependencyProperty MouseEnterImageProperty = DependencyProperty.Register("UCMouseEnterImgSource", typeof(ImageSource), typeof(ButtonUserControl), null);

        public event PropertyChangedEventHandler PropertyChanged;

        [Browsable(true)]
        public ImageSource ImgSource
        {
            get { return (ImageSource)GetValue(ImgSourceProperty); }
            set
            {
                SetValue(ImgSourceProperty, value);
                if (this.PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ImgSource"));
                }
            }
        }

        [Description("获取或设置鼠标进入时的图片")]
        [Category("Common Properties")]
        [Browsable(true)]
        public ImageSource MouseEnterImgSource
        {
            get { return (ImageSource)GetValue(MouseEnterImageProperty); }
            set
            {
                SetValue(MouseEnterImageProperty, value);
                if (this.PropertyChanged != null)
                {
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs("MouseEnterImgSource"));
                }
            }
        }

    }
}



上一篇:wordpress3.6 修改的地方

下一篇:个人代码全部开源


0 评论

查看所有评论

给个评论吧