如果光标位于元素上,则禁用滚动

时间:2013-02-15 13:32:21

标签: wpf scroll

我希望在光标位于WPF中的元素上时禁用滚动。 enter image description here

如果鼠标位于红色矩形上方,则应禁用滚动。 红色矩形位于滚动查看器上。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我让它使用这段代码:

<Window.Resources>
    <converter:MouseOverToScrollBarVisibility x:Key="scrollVisibility" />
</Window.Resources>
<ScrollViewer VerticalScrollBarVisibility="{Binding IsMouseOver,
                ElementName=rec,
                Converter={StaticResource scrollVisibility}}">
    <Rectangle Height="50" Width="50" Fill="Red" x:Name="rec"/>
</ScrollViewer>

然后定义这个转换器:

[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.Hidden : ScrollBarVisibility.Visible;
    }

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