因此,可以说我有一个WPF形式与几个文本框,如果标签的文本框,它已经有东西在里面,我想选择在框中的所有文本,以便打字将删除该文本。 如果包装盒上的你的鼠标点击,这可能意味着你要改变的地方的信,所以没有突出显示所有在这种情况下。 似乎很容易,但一个好的解决方案,至今躲避我。 这里是我到目前为止,这是非常接近的工作,但还不太完善。
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<EventSetter Event="GotKeyboardFocus" Handler="EventSetter_OnHandler" />
</Style>
private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
TextBox txt = sender as TextBox;
if (txt != null) txt.SelectAll();
}
所以,当box获得键盘焦点则选择全部,所以tab键切换到文本框中选择完美的所有文本。 但是,如果鼠标点击调用此方法为好,这也凸显了文本,但点击进而提出在鼠标点击后的光标。 因此在功能上它是完美的,但它仍然困扰着我,它选择的闪烁一切鼠标点击时。 没有更好的方法来做到这一点,或者把某种支票在我的事件要知道,我得到了键盘焦点从鼠标点击,而不是一个标签?
Answer 1:
还没有看到任何干净的解决方案至今遗憾的是,有一两件事你可以做的就是检查鼠标状态:
var tb = (TextBox)sender;
if (Mouse.LeftButton != MouseButtonState.Pressed)
tb.SelectAll();
但实际上是一个更好的办法,由于在关键的焦点转移下来,你可以检查键盘代替。 我会建议使用合适签名的GotKeyboardFocus
处理程序,以获得相应的事件ARGS:
private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
在这一点上,你还可以看到在点击一些选择是否清零,但是这仅仅是因为以前只有选择获取如果不聚焦隐藏。 您可以随时清除选择在LostKeyboardFocus
防止(例如((TextBox)sender).Select(0, 0)
Answer 2:
你可以尝试检查,如果鼠标出现在文本框时,焦点事件发生,检查鼠标ButtonButtonState。 这不是完美的,但应接近你在找什么。
private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
TextBox txt = sender as TextBox;
Point position = Mouse.GetPosition(txt);
// if Mouse position is not in the TextBox Client Rectangle
// and Mouse Button not Pressed.
if((!(new Rect(0,0,txt.Width,txt.Height)).Contains(position)) || ( Mouse.LeftButton != MouseButtonState.Pressed))
if (txt != null) txt.SelectAll();
}
和HB指出了,你可以尝试使用txt.IsMouseOver属性,以确定是否光标在文本框的客户端矩形内。 它看起来很干净。
private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
TextBox txt = sender as TextBox;
if( !txt.IsMouseOver || Mouse.LeftButton != MouseButtonState.Pressed)
if (txt != null) txt.SelectAll();
}
Answer 3:
你可以捕捉最后按下的键,并在事件比较反对
private Key lastKey;
protected override void OnKeyDown(KeyEventArgs e)
{
lastKey = e.Key;
base.OnKeyDown(e);
}
在您的活动:
private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
if(lastKey != Key.Tab)
return;
TextBox txt = sender as TextBox;
if (txt != null) txt.SelectAll();
}
这不是完美的,因为他们可以打标签(Tab键没有进入你的控制),比点击进入你的控制。 但它会工作的大部分时间。
Answer 4:
您可以使用附加的行为模式
public class Behaviors
{
public static readonly DependencyProperty SelectTextOnFocusProperty = DependencyProperty
.RegisterAttached("SelectTextOnFocus", typeof(bool), typeof(Behaviors), new FrameworkPropertyMetadata(false, GotFocus));
public static void SetSelectTextOnFocus(DependencyObject obj, bool value)
{
obj.SetValue(SelectTextOnFocusProperty, value);
}
private static void GotFocus(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textbox = d as TextBox;
if (null == textbox) return;
textbox.GotKeyboardFocus += SelectTextOnFocus;
textbox.GotMouseCapture += SelectTextOnFocus;
}
private static void SelectTextOnFocus(object sender, RoutedEventArgs e)
{
if (!(sender is TextBox)) return;
((TextBox)sender).SelectAll();
}
}
在XAML中只需要
xmlns:my="clr-namespace:Namespace;assembly=Rkmax"
使用您可以在一个TextBox使用像
<TextBox my:Behaviors.SelectTextOnFocus="True" />
所有这些工作的鼠标和键盘事件
Answer 5:
我搜索了很多的解决方案,我发现了几个解决方案,全选,但,问题是,当我们做右键,选择从文本框中的文本的一部分后做剪切/复制,它选择甚至是所有我所选文本的一部分。 在这里解决这个问题是解决方案。 只需在键盘选择事件中添加下面的代码。 这为我工作。
private static void SelectContentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox)
{
TextBox textBox = d as TextBox;
if ((e.NewValue as bool?).GetValueOrDefault(false))
{
textBox.GotKeyboardFocus += OnKeyboardFocusSelectText;
}
else
{
textBox.GotKeyboardFocus -= OnKeyboardFocusSelectText;
}
}
}
private static void OnKeyboardFocusSelectText(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
文章来源: Textbox SelectAll on tab but not mouse click