c# - 当鼠标悬停在 ScrollViewer 上时,为什么我的 WPF 样式触发器无法仅显示水平滚动条?

标签 c# .net wpf xaml scrollviewer

我想创建一个仅在鼠标悬停时显示水平滚动条的 ScrollView,因此我尝试了以下操作(请注意,当鼠标悬停时我只是尝试将 Horizo​​ntalScrollBarVisibility 触发为 Auto 并将我的 ScrollViewer 预定义为有一个禁用的 Horizo​​ntalScrollBarVisibility):

<Window x:Class="OnMouseOverStackPanel.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="Triggers" TargetType="ScrollViewer">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Content="I'm a content filler."/>
        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Style="{StaticResource Triggers}">
            <StackPanel Orientation="Horizontal" Height="30">
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
                <Button Content="Hey, I'm a button!"/>
            </StackPanel>
        </ScrollViewer>
        <Button Grid.Row="2" Content="I'm a content filler."/>
    </Grid>
</Window>

不幸的是,这对我不起作用。如何将触发器附加到此对象以使其工作?

最佳答案

如果您直接设置一个属性,那么它优先于 Style 因此在您的情况下 Style 永远无法修改 Horizo​​ntalScrollBarVisibility 直接在 ScrollViewer 上设置。

因此更改将是删除元素上的设置 Horizo​​ntalScrollBarVisibility="Disabled" ,而是通过 Style 设置默认值,并让触发器在应用时修改默认值.当您有触发器修改元素的属性时,指定 Style 默认值通常是一种很好的做法。您可以将默认值设置为 HiddenDisabled 或您选择的任何默认值。

尝试:

<Window.Resources>
  <Style x:Key="Triggers"
          TargetType="ScrollViewer">
    <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="true">
        <Setter Property="HorizontalScrollBarVisibility" Value="Auto" />
      </Trigger>
    </Style.Triggers>
  </Style>
</Window.Resources>
<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>
  <Button Grid.Row="0"
          Content="I'm a content filler." />
  <ScrollViewer Grid.Row="1"
                Style="{StaticResource Triggers}"
                VerticalScrollBarVisibility="Disabled">
    <StackPanel Height="30"
                Orientation="Horizontal">
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
      <Button Content="Hey, I'm a button!" />
    </StackPanel>
  </ScrollViewer>
  <Button Grid.Row="2"
          Content="I'm a content filler." />
</Grid>

关于c# - 当鼠标悬停在 ScrollViewer 上时,为什么我的 WPF 样式触发器无法仅显示水平滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051265/

相关文章:

c# - Interop.Excel - 使用 output.,Rows[i].Delete() 向上移动行,以便 for 循环不检查最近移动的行

c# - 确定 SSL/TLS 加密?

wpf 定义样式的自定义属性

wpf - 集成测试在 WPF MVVM 应用程序中异步调用 WCF 服务的 ViewModel

C# 图片 : Attempted to read or write protected memory

c# - 用于解析查询字符串、创建 URL 编码参数等的工具?

c# - 带 OrderBy 字数的 Linq 查询

c# - 如果从同一个线程调用 Dispatcher.BeginInvoke 是否会将调用排队?

android - 在 iOS 和 Android 中使用 .NET 公开的 OData 模式

c# - WPF ItemsControl 按钮命令绑定(bind)不起作用