using BPA.UIControl.Commons.KnownBoxes; 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 BPA.UIControl { /// /// 重命名工具 /// [TemplatePart(Name = TextBoxPartName, Type = typeof(TextBox))] public class Renamer : ContentControl { /// /// 文本框名称 /// public const string TextBoxPartName = "PART_TextBox"; private TextBox textBox; #region commands /// /// 重命名命令 /// public static RoutedCommand RenameCommand = new RoutedCommand(); /// /// 重命名完成命令 /// public static RoutedCommand RenameCompletedCommand = new RoutedCommand(); /// /// 重命名取消命令 /// public static RoutedCommand RenameCancelCommand = new RoutedCommand(); #endregion commands #region events /// /// 文本改变事件 /// public static readonly RoutedEvent TextChangedEvent = EventManager.RegisterRoutedEvent("TextChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(Renamer)); /// /// 文本改变事件 /// public event RoutedPropertyChangedEventHandler TextChanged { add { AddHandler(TextChangedEvent, value); } remove { RemoveHandler(TextChangedEvent, value); } } #endregion events #region properties /// /// 文本改变命令 /// public static readonly DependencyProperty TextChangedCommandProperty = DependencyProperty.Register("TextChangedCommand", typeof(ICommand), typeof(Renamer), new PropertyMetadata(default(ICommand))); /// /// 文本改变命令 /// public ICommand TextChangedCommand { get { return (ICommand)GetValue(TextChangedCommandProperty); } set { SetValue(TextChangedCommandProperty, value); } } /// /// 是否重命名中 /// public static readonly DependencyProperty IsRenamingProperty = DependencyProperty.Register( "IsRenaming", typeof(bool), typeof(Renamer), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsRenamingChanged)); /// /// 是否重命名中 /// public bool IsRenaming { get { return (bool)GetValue(IsRenamingProperty); } set { SetValue(IsRenamingProperty, BooleanBoxes.Box(value)); } } /// /// 文本 /// public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(Renamer), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged)); /// /// 文本 /// public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } #endregion properties static Renamer() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Renamer), new FrameworkPropertyMetadata(typeof(Renamer))); } /// public override void OnApplyTemplate() { base.OnApplyTemplate(); CommandBindings.Add(new CommandBinding(RenameCommand, RenameHandler)); CommandBindings.Add(new CommandBinding(RenameCompletedCommand, RenameCompletedHandler)); CommandBindings.Add(new CommandBinding(RenameCancelCommand, RenameCancelHandler)); textBox = GetTemplateChild(TextBoxPartName) as TextBox; textBox.LostFocus += TextBox_LostFocus; var enterKeyBinding = new KeyBinding(RenameCompletedCommand, Key.Enter, ModifierKeys.None); var escKeyBinding = new KeyBinding(RenameCancelCommand, Key.Escape, ModifierKeys.None); var f2KeyBinding = new KeyBinding(RenameCommand, Key.F2, ModifierKeys.None); textBox.InputBindings.Add(enterKeyBinding); textBox.InputBindings.Add(escKeyBinding); this.InputBindings.Add(f2KeyBinding); this.PreviewMouseUp += (sender, e) => { if (!IsRenaming) { //e.Handled = true; this.Focus(); } }; } private void RenameHandler(object sender, ExecutedRoutedEventArgs e) { IsRenaming = true; } private void RenameCompletedHandler(object sender, ExecutedRoutedEventArgs e) { IsRenaming = false; } private void RenameCancelHandler(object sender, ExecutedRoutedEventArgs e) { textBox.Text = Text; IsRenaming = false; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { IsRenaming = false; } private static void OnIsRenamingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var renamer = d as Renamer; if (renamer.IsRenaming) { renamer.textBox.Visibility = Visibility.Visible; renamer.textBox.Focus(); renamer.textBox.SelectAll(); } else { renamer.textBox.Visibility = Visibility.Collapsed; renamer.Focus(); } } private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var renamer = d as Renamer; var oldString = e.OldValue == null ? string.Empty : e.OldValue.ToString(); var newString = e.NewValue == null ? string.Empty : e.NewValue.ToString(); var args = new RoutedPropertyChangedEventArgs(oldString, newString); args.RoutedEvent = Renamer.TextChangedEvent; renamer.RaiseEvent(args); renamer.TextChangedCommand?.Execute(e.NewValue.ToString()); } } }