ListBox in ScrollViewer: mouse wheel not working

My mouse wheel does not work when installed ListBoxin ScrollViewer.

Somehow "t20" "stole" this event?

<ScrollViewer VerticalScrollBarVisibility="Auto" Style="{StaticResource myStyle}">
<ListBox>
  <ListBoxItem>Test 1</ListBoxItem>
  <ListBoxItem>Test 2</ListBoxItem>
  <ListBoxItem>Test 3</ListBoxItem>
  <ListBoxItem>Test 4</ListBoxItem>
  <ListBoxItem>Test 5</ListBoxItem>
  <ListBoxItem>Test 6</ListBoxItem>
  <ListBoxItem>Test 7</ListBoxItem>
</ListBox>
</ScrollViewer>

Edit: at the request of Joel, added the reason I did it. I did this because I don't like what the ListBoxinside does ScrollViewerwith my layout. I have a background image, and also ListBox, as shown below:

alt text http://robbertdam.nl/share/1.png

Now that the scroll bar appears, the following happens:

alt text http://robbertdam.nl/share/2.png

ScrollViewer, ListBox. ListBox .

,

+5
3

-, , , . , , , . - , , .

ListBox ScrollViewer, ListBox ScrollViewer , ListBox , , ScrollViewer ListBox. , ScrollViewer ListBox .

ListBox , ScrollViewer, , ListBox , : 1 ListBox 1 ListBox ScrollViewer. ListBox, ListBox ScrollViewer, Border . ListBox ScrollViewer, ScrollViewer - ListBox - , , ListBox Border .

, ScrollViewer ListBox ( Border, ), ListBox, ScrollViewer, , .

. , ScrollViewer ListBox, . , ListBox, ListBoxItem , , , ListBox VirtualizingStackPanel.

, , .


: , . , , , , , ScrollViewer Grid. , , :

  • ListBox, ScrollViewer, ScrollViewer ListBox. ListBox , Style, . , .
  • ListBox ControlTemplate ScrollViewer , , , . (ListBox VirtualizingStackPanel, yay), , , DataTemplate.
  • ScrollViewer, , . :

ScrollViewer 2 Grid:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

, Width 0, Width="Auto". , , Width Width :

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition
        Width="{Binding ElementName=PART_VerticalScrollBar, Path=Width}" />
</Grid.ColumnDefinitions>

, ControlTemplate Style ScrollViewer :

<ControlTemplate
    TargetType="{x:Type ScrollViewer}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition
                Width="{Binding ElementName=PART_VerticalScrollBar, Path=Width}" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition
                Height="Auto" />
        </Grid.RowDefinitions>

        <ScrollContentPresenter />

        <ScrollBar
            Grid.Column="1"
            Name="PART_VerticalScrollBar"
            Value="{TemplateBinding VerticalOffset}"
            Maximum="{TemplateBinding ScrollableHeight}"
            ViewportSize="{TemplateBinding ViewportHeight}"
            Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" />
        <ScrollBar
            Name="PART_HorizontalScrollBar"
            Orientation="Horizontal"
            Grid.Row="1"
            Value="{TemplateBinding HorizontalOffset}"
            Maximum="{TemplateBinding ScrollableWidth}"
            ViewportSize="{TemplateBinding ViewportWidth}"
            Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />

    </Grid>
</ControlTemplate>

Width="*", , . DataTemplate , , , .

, , example ControlTemplate ScrollViewer, . , ! ContentScrollPresenter.

+15

. ListBox.ItemsPanel ScrollViewer:

 <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
     <ListBox
     <ListBox.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Border>
                <ItemsPresenter />
            </Border>
        </ControlTemplate>
     </ListBox.Template>
     <ListBox.ItemsPanel>
         <ItemsPanelTemplate>
             <VirtualizingStackPanel />
         </ItemsPanelTemplate>
     </ListBox.ItemsPanel>
    <\ListBox>
<\ScrollViewer>

, .

+3

. : ScrollViewer ListBox

, , ListBoxs PreviewMouseWheel. , .

PreviewMouseWheel and mark the event as handled:
    private void ListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (e.Delta < 0)
        {
            scrollViewer1.LineDown();
        }
        else
        {
            scrollViewer1.LineUp();
        }
    }
0

All Articles