WPF:选择所有文本并将焦点设置为ComboBox的可编辑文本框 [英] WPF: Selecting all the text in and setting focus to a ComboBox's editable textbox

查看:3089
本文介绍了WPF:选择所有文本并将焦点设置为ComboBox的可编辑文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF ComboBox ,它的 IsEditable 属性绑定到一个视图模型,关闭。当它打开时,我想把焦点放在 ComboBox 并选择编辑 TextBox 中的所有文本。

I have a WPF ComboBox which has its IsEditable property bound to a view model which can switch it on and off. When it is switched on, I want to give focus to the ComboBox and select all the text in the edit TextBox.

我看不到最好的做法。如果我替换 ControlTemplate ,子类化 ComboBox 基类,并提供所需的属性,使用附加属性,其他方法?

I can't see the best to accomplish this. Should I replace the ControlTemplate, subclass the ComboBox base class, and provide the needed properties, use Attached Properties, or some other approach?

推荐答案

我有一个解决方案。

我使用 Model-View-ViewModel和Attached Property 方法的组合。

首先,您需要知道 系统,以便了解您的工作,以及知道您的命令。所以从Attached属性开始,我们开始设置一个IsFocused事件到我们想要集中和选择所有文本的组合框。

Firstly, you need to know the Messaging system in MVVM for this to work, as well as know your Commands. So starting with Attached Properties, we begin to set a IsFocused event to the combo box we would like to have focused and all text selected.

  #region Select On Focus

  public static bool GetSelectWhenFocused(DependencyObject obj)
  {
    return (bool)obj.GetValue(SelectWhenFocusedProperty);
  }

  public static void SetSelectWhenFocused(DependencyObject obj, bool value)
  {
    obj.SetValue(SelectWhenFocusedProperty, value);
  }

  // Using a DependencyProperty as the backing store for SelectWhenFocused.  This enables animation, styling, binding, etc...
  public static readonly DependencyProperty SelectWhenFocusedProperty =
      DependencyProperty.RegisterAttached("SelectWhenFocused", typeof(bool), typeof(EditableComboBox), new UIPropertyMetadata(OnSelectOnFocusedChanged));

  public static void OnSelectOnFocusedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  {
    bool SetProperty = (bool)args.NewValue;     

    var comboBox = obj as ComboBox;
    if (comboBox == null) return;

    if (SetProperty)
    {
      comboBox.GotFocus += GotFocused;
      Messenger.Default.Register<ComboBox>(comboBox, Focus);
    }
    else
    {
      comboBox.GotFocus -= GotFocused;
      Messenger.Default.Unregister<ComboBox>(comboBox, Focus);
    }
  }

  public static void GotFocused(object sender, RoutedEventArgs e)
  {
    var comboBox = sender as ComboBox;
    if(comboBox == null) return;

    var textBox = comboBox.FindChild(typeof(TextBox), "PART_EditableTextBox") as TextBox;
    if (textBox == null) return;

    textBox.SelectAll();
  }

  public static void Focus(ComboBox comboBox)
  {
    if(comboBox == null) return;
    comboBox.Focus();
  }
  #endregion

此代码显示的是当我们设置Attached属性 SelectWhenFocused to true 时,它会注册以侦听 GotFocused事件,并选择其中的所有文字。

What this code shows is when we set the Attached Property SelectWhenFocused to true, it will register to listen for the GotFocused Event and select all the text inside.

使用很简单:

<ComboBox
  IsEditable="True"
  ComboBoxHelper:EditableComboBox.SelectWhenFocused="True"
  x:Name="EditBox" />

现在我们需要一个按钮来设置当单击时ComboBox的焦点。

Now we need a button that will set the focus on the ComboBox when clicked on.

<Button
  Command="{Binding Focus}"
  CommandParameter="{Binding ElementName=EditBox}"
  Grid.Column="1" >Focus</Button>

注意 CommandParameter 如何通过名称EditBox绑定到ComboBox。

Notice how the CommandParameter is binding to the ComboBox by its name EditBox. This is so when the command executes, only this ComboBox gets focused and all text selected.

在我的ViewModel中,我使用了 Focus命令如下:

In my ViewModel, I have the Focus command declared as following:

public SimpleCommand Focus { get; set; }
public WindowVM()
{
  Focus = new SimpleCommand {ExecuteDelegate = x => Broadcast(x as ComboBox)};
}

这是一种经过测试和验证的技术,适用于我。我希望它是一个overkill解决你的问题。祝你好运。

This is a tested and proven technique that works for me. I hope its an overkill solution to your problem. Good luck.

这篇关于WPF:选择所有文本并将焦点设置为ComboBox的可编辑文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆