using System; using System.Windows; using System.Windows.Controls; //using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using CheckBox = System.Windows.Controls.CheckBox; using ComboBox = System.Windows.Controls.ComboBox; using OpenFileDialog = Microsoft.Win32.OpenFileDialog; using RadioButton = System.Windows.Controls.RadioButton; using RichTextBox = System.Windows.Controls.RichTextBox; using TextBox = System.Windows.Controls.TextBox; namespace Muchinfo.MTPClient.Infrastructure.Helpers { /// /// 公共附加属性 /// public static class ControlAttachPropertyHelper { /************************************ Attach Property **************************************/ #region WatermarkProperty 水印 /// /// 水印 /// public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached( "Watermark", typeof(string), typeof(ControlAttachPropertyHelper), new FrameworkPropertyMetadata("")); public static string GetWatermark(DependencyObject d) { return (string)d.GetValue(WatermarkProperty); } public static void SetWatermark(DependencyObject obj, string value) { obj.SetValue(WatermarkProperty, value); } #endregion /************************************ RoutedUICommand Behavior enable **************************************/ #region IsClearTextButtonBehaviorEnabledProperty 清除输入框Text值按钮行为开关(设为ture时才会绑定事件) /// /// 清除输入框Text值按钮行为开关 /// public static readonly DependencyProperty IsClearTextButtonBehaviorEnabledProperty = DependencyProperty.RegisterAttached("IsClearTextButtonBehaviorEnabled" , typeof(bool), typeof(ControlAttachPropertyHelper), new FrameworkPropertyMetadata(false, IsClearTextButtonBehaviorEnabledChanged)); [AttachedPropertyBrowsableForType(typeof(TextBox))] public static bool GetIsClearTextButtonBehaviorEnabled(DependencyObject d) { return (bool)d.GetValue(IsClearTextButtonBehaviorEnabledProperty); } public static void SetIsClearTextButtonBehaviorEnabled(DependencyObject obj, bool value) { obj.SetValue(IsClearTextButtonBehaviorEnabledProperty, value); } /// /// 绑定清除Text操作的按钮事件 /// private static void IsClearTextButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { //var button = d as FButton; //if (e.OldValue != e.NewValue && button != null) //{ // button.CommandBindings.Add(ClearTextCommandBinding); //} } #endregion /************************************ RoutedUICommand **************************************/ #region ClearTextCommand 清除输入框Text事件命令 /// /// 清除输入框Text事件命令,需要使用IsClearTextButtonBehaviorEnabledChanged绑定命令 /// public static RoutedUICommand ClearTextCommand { get; private set; } /// /// ClearTextCommand绑定事件 /// private static readonly CommandBinding ClearTextCommandBinding; /// /// 清除输入框文本值 /// private static void ClearButtonClick(object sender, ExecutedRoutedEventArgs e) { var tbox = e.Parameter as FrameworkElement; if (tbox == null) return; if (tbox is TextBox) { ((TextBox)tbox).Clear(); } //if (tbox is PasswordBox) //{ // ((PasswordBox)tbox).Clear(); //} //if (tbox is ComboBox) //{ // var cb = tbox as ComboBox; // cb.SelectedItem = null; // cb.Text = string.Empty; //} //if (tbox is MultiComboBox) //{ // var cb = tbox as MultiComboBox; // cb.SelectedItem = null; // cb.UnselectAll(); // cb.Text = string.Empty; //} //if (tbox is DatePicker) //{ // var dp = tbox as DatePicker; // dp.SelectedDate = null; // dp.Text = string.Empty; //} tbox.Focus(); } #endregion /// /// 静态构造函数 /// static ControlAttachPropertyHelper() { //ClearTextCommand ClearTextCommand = new RoutedUICommand(); ClearTextCommandBinding = new CommandBinding(ClearTextCommand); ClearTextCommandBinding.Executed += ClearButtonClick; //OpenFileCommand //OpenFileCommand = new RoutedUICommand(); //OpenFileCommandBinding = new CommandBinding(OpenFileCommand); //OpenFileCommandBinding.Executed += OpenFileButtonClick; ////OpenFolderCommand //OpenFolderCommand = new RoutedUICommand(); //OpenFolderCommandBinding = new CommandBinding(OpenFolderCommand); //OpenFolderCommandBinding.Executed += OpenFolderButtonClick; //SaveFileCommand = new RoutedUICommand(); //SaveFileCommandBinding = new CommandBinding(SaveFileCommand); //SaveFileCommandBinding.Executed += SaveFileButtonClick; } } }