【问题标题】:TextBox vertical scroll bar not working when applying IsHitTestVisible = False应用 IsHitTestVisible = False 时,TextBox 垂直滚动条不起作用
【发布时间】:2023-02-08 04:52:38
【问题描述】:

我有下面的 TextBox,它使用 Sytle 充当 TextBlock:

<Style x:Key="TextBlockStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="{x:Null}" />
    <Setter Property="BorderBrush" Value="{x:Null}" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="TextWrapping" Value="Wrap" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{x:Null}" />
        </Trigger>
    </Style.Triggers>
</Style>

<TextBox Grid.Column="1"
     BorderBrush="DarkRed"
     BorderThickness="1"
     Style="{StaticResource TextBlockStyle}"
     VerticalScrollBarVisibility="Auto"
     x:Name="MyTextBox"
     VerticalAlignment="Stretch"
     HorizontalAlignment="Stretch"
     Height="auto"
     Margin="5"
     Text="{Binding Path=Text}"
     Foreground="{Binding Path=ForegroundColor}">
</TextBox>

这种方法的问题是可以选择 TextBox 内容,但我不希望这样,所以在 Style 中我添加了一个新属性,如下所示:

<Setter Property="IsHitTestVisible" Value="False" />

所以现在无法选择 TextBox 内容。好的,这正是我想要的,但现在引入了新的副作用。现在,当垂直滚动条可见时,它处于只读模式,我无法单击垂直滚动条的向上和向下按钮。当我点击它们时,点击被忽略,什么也没有发生....所以当我点击向上和向下按钮时,我怎样才能使文本框内容不可选,同时使垂直滚动条工作?

【问题讨论】:

  • 您可以将 TextBox 包装在 ScrollViewer 中吗?
  • @IlanKeshet 您的意思是使用 ScrollViewer 而不是 VerticalScrollBarVisibility="Auto" 属性?像下面这样: <ScrollViewer><TextBox></TextBox></ScrollViewer>?
  • 您称您的样式为“TextBlockStyle”,但您使用的是 TextBox。为什么不直接使用 TextBlock 呢?
  • @Rodri 是的——但我不确定这是否真的有效

标签: c# wpf xaml textbox border


【解决方案1】:

不要设置文本框的 VerticalScrollBarVisibility,而是尝试在文本框上设置 ScrollViewer.VerticalScrollBarVisibility 附加属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2017-02-08
    • 1970-01-01
    相关资源
    最近更新 更多