简体   繁体   English

WPF-TextBox中的文本无法选择?

[英]WPF - Text in TextBox not Selectable?

I am working on a project that dynamically loads text into a Text Box control. 我正在一个将文本动态加载到文本框控件中的项目。 The problem is that, for some reason, the text does not seem as if it can be selected. 问题在于,由于某种原因,该文本看起来好像不能被选择。 I do not understand why as I thought that, by default, the text in a Text Box was selectable. 我不明白为什么我以为默认情况下文本框中的文本是可选的。 Why is this and how can I fix it? 为什么会这样,我该如何解决? Here is my XAML: 这是我的XAML:

 <Window x:Name="viewWindow" x:Class="Games_Database.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="View" Height="300" Width="600" Loaded="windowLoad" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="77*"/>
        <RowDefinition Height="13*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="57*"/>           
    </Grid.ColumnDefinitions>
    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Width="550" Margin="21,5,21,10" Grid.RowSpan="2" IsManipulationEnabled="True">
        <TextBox x:Name="text" HorizontalAlignment="Center" Margin="5,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" IsHitTestVisible="False" Height="Auto" Width="500" Grid.RowSpan="2" ScrollViewer.CanContentScroll="True" FontSize="14" FontFamily="Times New Roman" IsInactiveSelectionHighlightEnabled="True" Text="" AutoWordSelection="True" />
    </ScrollViewer>
</Grid>

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks much in advance! 在此先感谢!

The text cannot be selected because you have IsHitTestVisible="False" . 无法选择文本,因为您具有IsHitTestVisible="False"

If you remove that attribute, the text box will once again be able to respond to mouse clicks from the user. 如果删除该属性,则文本框将再次能够响应用户的鼠标单击。 Note that, with your current code, you can get the cursor inside the text box by pressing the tab key. 请注意,使用当前代码,您可以通过按Tab键将光标移到文本框中。

It's beacause you have IsHitTestVisible property set to false. 因为您将IsHitTestVisible属性设置为false。 Setting this property to false means that control will not respond on any mouse events. 将此属性设置为false意味着控件将不会对任何鼠标事件做出响应。 However you are still able to enter the control by using tab. 但是,您仍然可以使用选项卡进入控件。

Perhaps this blog post will make it more clear to you. 也许这篇博客文章会让您更清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM