WPF自定义控件

来源:互联网 发布:好听音乐网络歌曲 编辑:程序博客网 时间:2024/05/21 10:15
using System.Windows;using System.Windows.Controls;using System.Windows.Media;namespace ZJWMSUI.Controls{    public class DazzleButton : Button    {        public static readonly DependencyProperty MyMoverBrushProperty;        public static readonly DependencyProperty MyEnterBrushProperty;        public Brush MyMoverBrush        {            get            {                return base.GetValue(DazzleButton.MyMoverBrushProperty) as Brush;            }            set            {                base.SetValue(DazzleButton.MyMoverBrushProperty, value);            }        }        public Brush MyEnterBrush        {            get            {                return base.GetValue(DazzleButton.MyEnterBrushProperty) as Brush;            }            set            {                base.SetValue(DazzleButton.MyEnterBrushProperty, value);            }        }        static DazzleButton()        {            DazzleButton.MyMoverBrushProperty = DependencyProperty.Register("MyMoverBrush", typeof(Brush), typeof(DazzleButton), new PropertyMetadata(null));            DazzleButton.MyEnterBrushProperty = DependencyProperty.Register("MyEnterBrush", typeof(Brush), typeof(DazzleButton), new PropertyMetadata(null));            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(DazzleButton), new FrameworkPropertyMetadata(typeof(DazzleButton)));        }        public DazzleButton()        {            base.Content = "";            base.Background = Brushes.Orange;        }    }}

using System.Windows;using System.Windows.Controls;using System.Windows.Media;using System.ComponentModel;namespace ZJWMSUI.Controls{    public class XButton : Button    {        static XButton()        {            DefaultStyleKeyProperty.OverrideMetadata(typeof(XButton), new FrameworkPropertyMetadata(typeof(XButton)));            var metadata = new FrameworkPropertyMetadata((ImageSource)null);            ImageSourceProperty = DependencyProperty.RegisterAttached("ImageSource", typeof(ImageSource), typeof(Button), metadata);        }        #region 属性               public ImageSource ImageSource        {            get { return (ImageSource)GetValue(ImageSourceProperty); }            set { SetValue(ImageSourceProperty, value); }        }        public static  DependencyProperty ImageSourceProperty;        #endregion        #region 隐藏基类属性        [Browsable(false)]        public new Brush Background        {            get { return base.Background; }            set { base.Background = value; }        }        [Browsable(false)]        public new Brush BorderBrush        {            get { return base.BorderBrush; }            set { base.BorderBrush = value; }        }        #endregion    }}
using System.Windows;using System.Windows.Controls;using System.Windows.Media;using System.ComponentModel;namespace ZJWMSUI.Controls{    /// <summary>    /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。    ///    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根     /// 元素中:     ///    ///     xmlns:MyNamespace="clr-namespace:WpfApplication3"    ///    ///    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根     /// 元素中:     ///    ///     xmlns:MyNamespace="clr-namespace:WpfApplication3;assembly=WpfApplication3"    ///    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,    /// 并重新生成以避免编译错误:     ///    ///     在解决方案资源管理器中右击目标项目,然后依次单击    ///     “添加引用”->“项目”->[浏览查找并选择此项目]    ///    ///    /// 步骤 2)    /// 继续操作并在 XAML 文件中使用控件。    ///    ///     <MyNamespace:XTextBox/>    ///    /// </summary>    public class XTextBox : TextBox    {        static XTextBox()        {            DefaultStyleKeyProperty.OverrideMetadata(typeof(XTextBox), new FrameworkPropertyMetadata(typeof(XTextBox)));            var metadata = new FrameworkPropertyMetadata((ImageSource)null);            ImageSourceProperty = DependencyProperty.RegisterAttached("ImageSource", typeof(ImageSource), typeof(TextBox), metadata);            var metadata1 = new FrameworkPropertyMetadata((string)"");            XTextBox.ContentTextProperty = DependencyProperty.RegisterAttached("ContentText", typeof(string), typeof(TextBox), metadata1);        }        public ImageSource ImageSource        {            get { return (ImageSource)GetValue(ImageSourceProperty); }            set { SetValue(ImageSourceProperty, value); }        }        public static readonly DependencyProperty ImageSourceProperty;        public string ContentText        {            get { return (string)GetValue(ContentTextProperty); }            set { SetValue(ContentTextProperty, value); }        }        public static readonly DependencyProperty ContentTextProperty;        #region 隐藏基类属性        [Browsable(false)]        public new Brush Background        {            get { return base.Background; }            set { base.Background = value; }        }        [Browsable(false)]        public new Brush BorderBrush        {            get { return base.BorderBrush; }            set { base.BorderBrush = value; }        }        #endregion    }}



0 0
原创粉丝点击