繁体   English   中英

ScrollViewer不适用于GroupBox上的MouseWheel

[英]ScrollViewer not working with MouseWheel on GroupBox

我试图让scrollviewer在自定义样式的groupbox中工作。 这是分组框的样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<!--Set default style of groupbox-->
<Style TargetType="GroupBox">
    <Setter Property="Margin" Value="0, 10, 0, 0"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GroupBox">
                <Border CornerRadius="4" BorderThickness="1" BorderBrush="{StaticResource BorderBrush}" Background="{StaticResource ContentBackgroundBrush}">
                    <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                        <StackPanel Orientation="Vertical" CanVerticallyScroll="True">
                            <Label Content="{TemplateBinding Header}" Margin="5,5,0,0" Style="{StaticResource SmallTitle}"></Label>
                            <ContentPresenter Margin="10, 5, 10, 10" RecognizesAccessKey="True" x:Name="CtlGroupboxPresenter" />
                        </StackPanel>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

滚动条显示出来,但是我无法使用鼠标滚轮滚动。 但是,当我的鼠标悬停在垂直滚动条上时,它将起作用。 似乎是一个跟踪问题。

我看到SO上的一些人建议在代码后面添加一些代码以使其正常工作,但是由于这是资源字典,所以我无处可放...

有人知道这个问题是什么吗?

这是wpf形式的图像: 在此处输入图片说明

组框内的XAML:

<UserControl x:Class="Sun.Plasma.Controls.ViewNews"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<DockPanel LastChildFill="True">
    <Label DockPanel.Dock="Top" Style="{StaticResource LblTitle}" FontWeight="Bold" FontSize="24" >Latest SUN news &amp; announcements</Label>

    <StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
        <StackPanel Orientation="Vertical" Name="CtlLoadingNews">
            <Label Style="{StaticResource LblContent}">Loading content from server...</Label>
            <ProgressBar IsIndeterminate="True" Height="30" />
        </StackPanel>
        <ListView Background="Transparent" DockPanel.Dock="Bottom" ItemsSource="{Binding NewsFeeds}" BorderBrush="Transparent" Name="CtlNews" Visibility="Collapsed">

            <!-- Defining these resources prevents the items from appearing as selectable -->
            <ListView.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            </ListView.Resources>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Margin="0 0 0 20">
                        <Label Style="{StaticResource LblTitle}" FontWeight="Bold" Content="{Binding Title}" />
                        <StackPanel Orientation="Horizontal">
                            <Label Style="{StaticResource LblFooter}" Content="{Binding PublishDate}" />
                            <Label Style="{StaticResource LblFooter}">By</Label>
                            <Label Style="{StaticResource LblFooter}" Content="{Binding Authors[0]}" />
                            <Label Style="{StaticResource LblFooter}">
                                <Hyperlink RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="{Binding Source}">Read entry</Hyperlink>
                            </Label>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</DockPanel>

问题是GroupBox内容中的ListView阻止了MouseWheel事件冒泡到ScrollViewer 我发现了一个骇人的解决方案:

您可以在内部ListView上处理PreviewMouseWheel事件,并直接在滚动查看器上引发MouseWheel事件。

private void ListView_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if (!e.Handled)
    {
        e.Handled = true;
        var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
        eventArg.RoutedEvent = UIElement.MouseWheelEvent;
        eventArg.Source = sender;
        //navigate to the containing scrollbar and raise the MouseWheel event
        (((sender as ListView).Parent as GroupBox).Content as ListView).RaiseEvent(eventArg);
    }
}

同样,这不是我特别喜欢的解决方案,因为它取决于GroupBox的布局。

第二种更好的方法是在GroupBox的资源中添加样式,在其中向PreviewMouseWheel事件添加处理程序:

<GroupBox Header="test">
    <GroupBox.Resources>
        <Style TargetType="ScrollViewer">
            <EventSetter Event="PreviewMouseWheel" Handler="ScrollViewer_PreviewMouseWheel" />
        </Style>
    </GroupBox.Resources>
    <!-- your contents -->
</GroupBox>

然后,事件处理程序进行滚动:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    var scrollViewer = sender as ScrollViewer;
    double change = e.Delta;
    double currentPosition = scrollViewer.VerticalOffset;

    scrollViewer.ScrollToVerticalOffset(currentPosition - change);
}

暂无
暂无

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

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