簡體   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