|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- 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 BPASmartClient.CustomResource.UserControls
- {
- /// <summary>
- /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
- ///
- /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
- /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
- /// 元素中:
- ///
- /// xmlns:MyNamespace="clr-namespace:BPASmartClient.CustomResource.UserControls"
- ///
- ///
- /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
- /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
- /// 元素中:
- ///
- /// xmlns:MyNamespace="clr-namespace:BPASmartClient.CustomResource.UserControls;assembly=BPASmartClient.CustomResource.UserControls"
- ///
- /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
- /// 并重新生成以避免编译错误:
- ///
- /// 在解决方案资源管理器中右击目标项目,然后依次单击
- /// “添加引用”->“项目”->[浏览查找并选择此项目]
- ///
- ///
- /// 步骤 2)
- /// 继续操作并在 XAML 文件中使用控件。
- ///
- /// <MyNamespace:IcoButton/>
- ///
- /// </summary>
- public class IcoButton : Button
- {
- static IcoButton()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(IcoButton), new FrameworkPropertyMetadata(typeof(IcoButton)));
- }
-
- private Brush tempBackground;
-
- private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as IcoButton)?.Refresh();
- }
-
- private void Refresh()
- {
- tempBackground = this.Background;
- }
-
- //public Action Command
- //{
- // get { return (Action)GetValue(CommandProperty); }
- // set { SetValue(CommandProperty, value); }
- //}
- //public static readonly DependencyProperty CommandProperty =
- // DependencyProperty.Register("Command", typeof(Action), typeof(IcoButton),
- // new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
-
-
- //public Action<object> CommandObj
- //{
- // get { return (Action<object>)GetValue(CommandObjProperty); }
- // set { SetValue(CommandObjProperty, value); }
- //}
- //public static readonly DependencyProperty CommandObjProperty =
- // DependencyProperty.Register("CommandObj", typeof(Action<object>), typeof(IcoButton),
- // new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
-
-
- //public object CommandPar
- //{
- // get { return (object)GetValue(CommandParProperty); }
- // set { SetValue(CommandParProperty, value); }
- //}
- //public static readonly DependencyProperty CommandParProperty =
- // DependencyProperty.Register("CommandPar", typeof(object), typeof(IcoButton),
- // new PropertyMetadata(default, new PropertyChangedCallback(OnPropertyChanged)));
-
-
-
- //public string Content
- //{
- // get { return (string)GetValue(ContentProperty); }
- // set { SetValue(ContentProperty, value); }
- //}
- //public static readonly DependencyProperty ContentProperty =
- // DependencyProperty.Register("Content", typeof(string), typeof(IcoButton),
- // new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnPropertyChanged)));
-
- public string IcoText
- {
- get { return (string)GetValue(IcoTextProperty); }
- set { SetValue(IcoTextProperty, value); }
- }
- public static readonly DependencyProperty IcoTextProperty =
- DependencyProperty.Register("IcoText", typeof(string), typeof(IcoButton),
- new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnPropertyChanged)));
-
- public Brush EnterBackground
- {
- get { return (Brush)GetValue(EnterBackgroundProperty); }
- set { SetValue(EnterBackgroundProperty, value); }
- }
- public static readonly DependencyProperty EnterBackgroundProperty =
- DependencyProperty.Register("EnterBackground", typeof(Brush), typeof(IcoButton),
- new PropertyMetadata(Brushes.Transparent, new PropertyChangedCallback(OnPropertyChanged)));
-
-
- public Brush PressedBackground
- {
- get { return (Brush)GetValue(PressedBackgroundProperty); }
- set { SetValue(PressedBackgroundProperty, value); }
- }
- public static readonly DependencyProperty PressedBackgroundProperty =
- DependencyProperty.Register("PressedBackground", typeof(Brush), typeof(IcoButton),
- new PropertyMetadata(Brushes.Gray, new PropertyChangedCallback(OnPropertyChanged)));
-
-
- public Brush EnableColor
- {
- get { return (Brush)GetValue(EnableColorProperty); }
- set { SetValue(EnableColorProperty, value); }
- }
- public static readonly DependencyProperty EnableColorProperty =
- DependencyProperty.Register("EnableColor", typeof(Brush), typeof(IcoButton),
- new PropertyMetadata(Brushes.Gray, new PropertyChangedCallback(OnPropertyChanged)));
-
-
-
- public override void OnApplyTemplate()
- {
- base.OnApplyTemplate();
- var icon = base.GetTemplateChild("PART_icoText") as TextBlock;
- var content = base.GetTemplateChild("PART_content") as TextBlock;
- var gr = base.GetTemplateChild("PART_gr") as Grid;
- //if (!this.IsEnabled)
- //{
- // this.Foreground = EnableColor;
- // this.BorderBrush = EnableColor;
- //}
- //this.IsEnabledChanged += IcoButton_IsEnabledChanged;
- if (icon != null)
- {
- Binding binding = new Binding("IcoText");
- binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
- binding.RelativeSource = new RelativeSource() { AncestorType = typeof(IcoButton), Mode = RelativeSourceMode.FindAncestor };
- icon.SetBinding(TextBlock.TextProperty, binding);
- }
-
- if (content != null)
- {
- Binding binding = new Binding("Content");
- binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
- binding.RelativeSource = new RelativeSource() { AncestorType = typeof(IcoButton), Mode = RelativeSourceMode.FindAncestor };
- content.SetBinding(TextBlock.TextProperty, binding);
- }
-
- if (gr != null)
- {
- //gr.MouseLeftButtonDown += Gr_MouseLeftButtonDown;
- gr.MouseEnter += Gr_MouseEnter;
- gr.MouseLeave += Gr_MouseLeave;
- }
- }
-
- private void IcoButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
- {
- //if (!this.IsEnabled)
- //{
- // this.Foreground = EnableColor;
- // this.BorderBrush = EnableColor;
- //}
- }
-
- /// <summary>
- /// 鼠标离开颜色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Gr_MouseLeave(object sender, MouseEventArgs e)
- {
- //VisualStateManager.GoToState(this, "Leave", true);
- this.Background = tempBackground;
- }
-
- /// <summary>
- /// 鼠标进入颜色
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Gr_MouseEnter(object sender, MouseEventArgs e)
- {
- //VisualStateManager.GoToState(this, "Enter", true);
- this.Background = EnterBackground;
- }
-
- private void Gr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- Command?.Execute(null);
- //this.Background = PressedBackground;
- }
-
- }
- }
|