UWP - 在文本框失去焦点时显示文本框文本高亮

3

当文本框失去焦点时,如何防止文本框隐藏所选文本的高亮显示?以下行适用于WPF。

textBox1.IsInactiveSelectionHighlightEnabled = true;

但是 UWP 有什么相应的等效物呢?
2个回答

1
你可以在Xaml或者代码中设置SelectionHighlightColorWhenNotFocused属性。你可以将其设置为任何颜色,我只是使用绑定来确保它与SelectionHighlightColor颜色相同,以便更容易。
<TextBox Style="{StaticResource TextBoxLightStyle}" Name="TextBoxMain" 
     AcceptsReturn="True"
     SelectionHighlightColorWhenNotFocused="{Binding SelectionHighlightColor, ElementName=TextBoxMain, Mode=OneWay}">
</TextBox>

0
据我所知,在UWP中没有相应的功能。其中一个可能的解决方案是使用一些图像来保持选择突出显示。以下是示例代码:

XAML:

<Border BorderThickness="2" BorderBrush="{ThemeResource TextBoxBorderThemeBrush}" Height="164" Width="684">
    <TextBox x:Name="textBox" TextWrapping="Wrap" Text="TextBox" BorderThickness="0,0,0,0"/>
</Border>

C#:

    private async void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
    {
        // clear background            
        textBox.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 255, 255, 255)); ;

        // render image
        RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(textBox);

        // set background
        textBox.Background = new ImageBrush()
        {
            ImageSource = renderTargetBitmap
        };
    }

重点关注: 输入图像描述

不重点关注: 输入图像描述

附言:我正在更新SelectionChanged事件的背景,但实际上您可以在LostFocus事件上创建图像并仅在该事件上更新。这应该更有效率。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接