C# 不聚焦时如何保持WPF TextBox选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/642498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep WPF TextBox selection when not focused?
提问by Dmitri Nesteruk
I want to show a selection in a WPF TextBox even when it's not in focus. How can I do this?
我想在 WPF TextBox 中显示一个选择,即使它不在焦点上。我怎样才能做到这一点?
采纳答案by John Myczek
I have used this solution for a RichTextBox, but I assume it will also work for a standard text box. Basically, you need to handle the LostFocus event and mark it as handled.
我已将此解决方案用于 RichTextBox,但我认为它也适用于标准文本框。基本上,您需要处理 LostFocus 事件并将其标记为已处理。
protected void MyTextBox_LostFocus(object sender, RoutedEventArgs e)
{
// When the RichTextBox loses focus the user can no longer see the selection.
// This is a hack to make the RichTextBox think it did not lose focus.
e.Handled = true;
}
The TextBox will not realize it lost the focus and will still show the highlighted selection.
TextBox 不会意识到它失去了焦点,仍然会显示突出显示的选择。
I'm not using data binding in this case, so it may be possible that this will mess up the two way binding. You may have to force binding in your LostFocus event handler. Something like this:
在这种情况下,我没有使用数据绑定,因此这可能会弄乱双向绑定。您可能必须在 LostFocus 事件处理程序中强制绑定。像这样的东西:
Binding binding = BindingOperations.GetBinding(this, TextProperty);
if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default ||
binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
{
BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource();
}
回答by Zamboni
Another option is to define a separate focus scope in XAML to maintain the selection in the first TextBox.
另一种选择是在 XAML 中定义一个单独的焦点范围,以维护第一个 TextBox 中的选择。
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="Text that does not loose selection."/>
<StackPanel Grid.Row="1" FocusManager.IsFocusScope="True">
<TextBox Text="Some more text here." />
<Button Content="Run" />
<Button Content="Review" />
</StackPanel>
</Grid>
回答by AiminHan
public class CustomRichTextBox : RichTextBox
{
protected override void OnLostFocus(RoutedEventArgs e)
{
}
}
回答by arolson101
I found that the suggestions listed (add a LostFocus handler, defining a FocusScope) to not work, but I did come across the code listed here: http://naracea.com/2011/06/26/selection-highlight-and-focus-on-wpf-textbox/, which creates a custom Adorner that highlights the text when not focused.
我发现列出的建议(添加一个 LostFocus 处理程序,定义一个 FocusScope)不起作用,但我确实遇到了这里列出的代码:http: //naracea.com/2011/06/26/selection-highlight-and- focus-on-wpf-textbox/,它创建一个自定义装饰器,在未聚焦时突出显示文本。
回答by Makeman
TextBoxBase.IsInactiveSelectionHighlightEnabledProperty has available since .NET Framework 4.5
TextBoxBase.IsInactiveSelectionHighlightEnabled属性自 .NET Framework 4.5 起可用
public bool IsInactiveSelectionHighlightEnabled { get; set; }