2015-05-09 38 views
0

我想让scrollviewer在自定义样式的groupbox中工作。 这是组框的样式:ScrollViewer不能在GroupBox上使用MouseWheel

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

滚动条显示出来,但使用鼠标滚轮我不能滚动。但是,当我的鼠标移动到垂直滚动条上时它可以正常工作。这似乎是一个跟踪问题。

我看到所以有些家伙建议加入一些代码,后面的代码得到它的工作,但因为这是一个资源字典我没有地方,我可以把它...

有谁知道这个问题是什么?

这里是WPF形式的图像: enter image description here

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> 

+0

您能分享GroupBox内容的XAML吗? – Domysee

+0

@Dominik完成:) – RononDex

回答

1

的问题是,在GroupBox内容的ListView停止MouseWheel从冒泡到ScrollViewer。我发现了一个hacky解决方案:

您处理内部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); 
} 
+0

感谢这个制定良好的答案!我会尝试第二种方法。 – RononDex

+0

完美工作,非常感谢您的努力:) – RononDex