更改鼠标悬停时 DataGrid 滚动条的可见性?

发布于 2025-01-03 00:14:43 字数 134 浏览 2 评论 0原文

如何在 DataGrid 的 IsMouseOver 属性上切换 DataGrid 滚动条的可见性?

我想让滚动条在鼠标悬停在 DataGrid 上时可见,而在鼠标移出 DataGrid 时不可见。如果有 XAML 示例,我们将不胜感激。

How can I toggle the visibility of a DataGrid scrollbar on the IsMouseOver property of DataGrid?

I want to make the scrollbar visible when the mouse hovers over the DataGrid and invisible when the mouse goes out of the DataGrid. A XAML example would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-小熊_ 2025-01-10 00:14:43

我会应用一种新的样式,如下所示:

<Style x:Key="dataGridStyle" TargetType={x:Type DataGrid}>
   <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
   <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
       </Trigger>
   </Style.Triggers>
 </Style>

I would apply a new style, something like this:

<Style x:Key="dataGridStyle" TargetType={x:Type DataGrid}>
   <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
   <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
       </Trigger>
   </Style.Triggers>
 </Style>
写给空气的情书 2025-01-10 00:14:43

创建一个值转换器:

[ValueConversion(typeof(bool), typeof(ScrollBarVisibility))]
sealed class MouseOverToScrollBarVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((bool)value) ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

在资源中的某处声明它:

<Window.Resources>
    <local:MouseOverToScrollBarVisibility x:Key="MouseOverToScrollBarVisibility" />
</Window.Resources>

并使用它将 ScrollViewer.VerticalScrollBarVisibility 属性绑定到 IsMouseOver 属性:

<DataGrid ScrollViewer.VerticalScrollBarVisibility=
          "{Binding IsMouseOver,
                    RelativeSource={RelativeSource Self},
                    Converter={StaticResource MouseOverToScrollBarVisibility}}">
</DataGrid>

Create a value converter:

[ValueConversion(typeof(bool), typeof(ScrollBarVisibility))]
sealed class MouseOverToScrollBarVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((bool)value) ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Declare it somewhere in resources:

<Window.Resources>
    <local:MouseOverToScrollBarVisibility x:Key="MouseOverToScrollBarVisibility" />
</Window.Resources>

And use it to bind ScrollViewer.VerticalScrollBarVisibility property to IsMouseOver property:

<DataGrid ScrollViewer.VerticalScrollBarVisibility=
          "{Binding IsMouseOver,
                    RelativeSource={RelativeSource Self},
                    Converter={StaticResource MouseOverToScrollBarVisibility}}">
</DataGrid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文