2015-05-09 2 views
0

Я пытаюсь заставить scrollviewer работать в пользовательском стиле групповом ящике. Это стиль для GroupBox:ScrollViewer не работает с MouseWheel на 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: enter image description here

XAML внутри 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> 

+0

Не могли бы вы поделиться XAML содержимого GroupBox? – Domysee

+0

@ Dominik done :) – RononDex

ответ

1

Проблема, что ListView в содержимом GroupBox останавливает MouseWheel событие от барботажа до ScrollViewer. Я нашел хакерское решение:

Вы обрабатываете событие 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); 
} 
+0

Спасибо за этот хорошо сформулированный ответ! Я попробую второй метод. – RononDex

+0

Отлично работает, спасибо за ваше усилие :) – RononDex

Смежные вопросы