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 { /// /// 按照步骤 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 文件中使用控件。 /// /// /// /// public class IcoButton : Control { 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 CommandObj { get { return (Action)GetValue(CommandObjProperty); } set { SetValue(CommandObjProperty, value); } } public static readonly DependencyProperty CommandObjProperty = DependencyProperty.Register("CommandObj", typeof(Action), 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.DarkGray, 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; //} } /// /// 鼠标离开颜色 /// /// /// private void Gr_MouseLeave(object sender, MouseEventArgs e) { //VisualStateManager.GoToState(this, "Leave", true); this.Background = tempBackground; } /// /// 鼠标进入颜色 /// /// /// private void Gr_MouseEnter(object sender, MouseEventArgs e) { //VisualStateManager.GoToState(this, "Enter", true); this.Background = EnterBackground; } private void Gr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (Command != null) Command(); this.Background = PressedBackground; } } }