gpt4 book ai didi

wpf - 初始焦点和全选行为

转载 作者:行者123 更新时间:2023-12-04 13:17:27 29 4
gpt4 key购买 nike

我有一个嵌套在窗口中的用户控件,该窗口充当对话框显示的 shell 。我忽略了 shell 窗口中的焦点,在托管用户控件中,我使用 FocusManager 将初始焦点设置为命名元素(文本框),如下所示。

这是有效的,将光标设置在命名文本框的开头;但是我希望选择所有文本。

TextBoxSelectionBehavior 类(如下)通常就是这样做的,但在这种情况下不是。是否有一个简单的 xaml 修复程序来获取在初始焦点上选择的命名文本框中的文本?

干杯,
贝瑞尔

文本框选择行为

// in app startup
TextBoxSelectionBehavior.RegisterTextboxSelectionBehavior();

/// <summary>
/// Helper to select all text in the text box on entry
/// </summary>
public static class TextBoxSelectionBehavior
{
public static void RegisterTextboxSelectionBehavior()
{
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent, new RoutedEventHandler(OnTextBox_GotFocus));
}

private static void OnTextBox_GotFocus(object sender, RoutedEventArgs e)
{
var tb = (sender as TextBox);
if (tb != null)
tb.SelectAll();
}
}

托管的用户控件
<UserControl   
<DockPanel KeyboardNavigation.TabNavigation="Local"
FocusManager.FocusedElement="{Binding ElementName=tbLastName}" >

<TextBox x:Name="tbLastName" ... />

止损解决方案

根据下面与 Rachel 的评论,我放弃了 FocusManger,转而使用一些代码:
tbLastName.Loaded += (sender, e) => tbLastName.Focus();

尽管如此,仍然会喜欢一种简单而常见的家务的声明式方法......

最佳答案

我通常使用 AttachedProperty使 TextBoxes 在焦点上突出显示其文本。它被用作

<TextBox local:HighlightTextOnFocus="True" />

附加属性(property)代码
public static readonly DependencyProperty HighlightTextOnFocusProperty =
DependencyProperty.RegisterAttached("HighlightTextOnFocus",
typeof(bool), typeof(TextBoxProperties),
new PropertyMetadata(false, HighlightTextOnFocusPropertyChanged));


[AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
[AttachedPropertyBrowsableForType(typeof(TextBox))]
public static bool GetHighlightTextOnFocus(DependencyObject obj)
{
return (bool)obj.GetValue(HighlightTextOnFocusProperty);
}

public static void SetHighlightTextOnFocus(DependencyObject obj, bool value)
{
obj.SetValue(HighlightTextOnFocusProperty, value);
}

private static void HighlightTextOnFocusPropertyChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var sender = obj as UIElement;
if (sender != null)
{
if ((bool)e.NewValue)
{
sender.GotKeyboardFocus += OnKeyboardFocusSelectText;
sender.PreviewMouseLeftButtonDown += OnMouseLeftButtonDownSetFocus;
}
else
{
sender.GotKeyboardFocus -= OnKeyboardFocusSelectText;
sender.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDownSetFocus;
}
}
}

private static void OnKeyboardFocusSelectText(
object sender, KeyboardFocusChangedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
textBox.SelectAll();
}
}

private static void OnMouseLeftButtonDownSetFocus(
object sender, MouseButtonEventArgs e)
{
TextBox tb = FindAncestor<TextBox>((DependencyObject)e.OriginalSource);

if (tb == null)
return;

if (!tb.IsKeyboardFocusWithin)
{
tb.Focus();
e.Handled = true;
}
}

static T FindAncestor<T>(DependencyObject current)
where T : DependencyObject
{
current = VisualTreeHelper.GetParent(current);

while (current != null)
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
};
return null;
}

编辑

根据下面的评论,摆脱 FocusManager.FocusedElement 怎么样?和设置 tb.Focus()tb.SelectAll()Loaded您的事件 TextBox ?

关于wpf - 初始焦点和全选行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7808974/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com