ScrollViewer无法在GroupBox上使用MouseWheel

时间:2015-05-09 11:30:14

标签: c# .net wpf

我想让一个scrollviewer在自定义样式的组框中工作。 这是groupbox的样式:

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

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

我在SO上看到一些人建议在代码后面添加一些代码以使其正常工作,但由于这是在资源字典中,我没有地方可以把它放在...

有谁知道这是什么问题?

这是wpf表单的图片: enter image description here

组框内的XAML:

@function escape_leading_numbers($s) {
  $first-char: str_slice(#{$s}, 0, 1);
  $found: index('1' '2' '3' '4' '5' '6' '7' '8' '9' '0', $first-char);
  @return if($found, unquote(str-insert(str-slice(#{$s}, 2), "\\3#{$first-char} ", 1)), $s);
}

$name: '007';

.#{escape_leading_numbers($name)} {
  color: red;
}

@each $car in
  bmwwhite
  hondared
  22ltr-porche
  30ltr-cossworth
 {
  .#{escape_leading_numbers($car)} {
    background:url(/img/cars/#{$car}.jpg) no-repeat
  }
 }

1 个答案:

答案 0 :(得分:1)

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

您处理内部PreviewMouseWheel上的ListView事件,并直接在滚动查看器上提升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);
}