| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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
- {
- /// <summary>
- /// 公共附加属性
- /// </summary>
- public static class ControlAttachPropertyHelper
- {
- /************************************ Attach Property **************************************/
- #region WatermarkProperty 水印
- /// <summary>
- /// 水印
- /// </summary>
- 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时才会绑定事件)
- /// <summary>
- /// 清除输入框Text值按钮行为开关
- /// </summary>
- 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);
- }
- /// <summary>
- /// 绑定清除Text操作的按钮事件
- /// </summary>
- 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事件命令
- /// <summary>
- /// 清除输入框Text事件命令,需要使用IsClearTextButtonBehaviorEnabledChanged绑定命令
- /// </summary>
- public static RoutedUICommand ClearTextCommand { get; private set; }
- /// <summary>
- /// ClearTextCommand绑定事件
- /// </summary>
- private static readonly CommandBinding ClearTextCommandBinding;
- /// <summary>
- /// 清除输入框文本值
- /// </summary>
- 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
- /// <summary>
- /// 静态构造函数
- /// </summary>
- 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;
- }
- }
- }
|