C# – wpf scrollviewer mousewheel not working with stackpanel

cmousewheelscrollviewerstackpanelwpf-4.0

I have a stackpanel wrapped with a scrollviewer. inside the stackpanel I've some grids and inside the grids there're stackpanels again and some tiles control by MahApps Metro.

the scrollviewer is working fine if I drag the scrollbar. but mousewheel not working. may be some control is stealing the mousewheel action but I can't feagure out which one. I've tried to scroll mouse wheel while focusing on the scrollbar. but it's still not working.

<ScrollViewer x:Name="TS" Grid.Row="1" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" Background="Transparent">
    <StackPanel x:Name="TilesPanel" VerticalAlignment="Top" HorizontalAlignment="Stretch" Orientation="Horizontal">
        <StackPanel.Resources>
            <Style TargetType="{x:Type Grid}">
                <Setter Property="Margin" Value="0,50,0,0"/>
            </Style>
        </StackPanel.Resources>
        <Separator Background="{x:Null}" Width="110"></Separator>
        <Grid>
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <StackPanel.Resources>
                        <Style TargetType="{x:Type Grid}">
                            <Setter Property="Margin" Value="10,0,0,0"/>
                        </Style>
                    </StackPanel.Resources>
                    <Grid Height="260">
                        <StackPanel>
                            <StackPanel.Resources>
                                <Style TargetType="{x:Type StackPanel}">
                                    <Setter Property="Margin" Value="0,0,0,10"/>
                                </Style>
                            </StackPanel.Resources>
                        <StackPanel Width="247" Height="119">
                            <Custom:Tile x:Name="Mail" Margin="0" Width="auto" d:LayoutOverrides="Height">
                                <Image Stretch="Fill" Source="Res/AppTiles/Mail.png"/>
                            </Custom:Tile>
                        </StackPanel>

                        //and it goes on like this//

                        </grid>
                      </stackpanel>
                <Separator Background="{x:Null}" Width="50"/>
            </Grid>
    </StackPanel>
    </ScrollViewer>

Is the grid that's coming in the way? Or is there any other way to use mousewheel in Horizontal Scrollviewer? I just can't figure out.
please help.

Best Answer

Yes I figured that out. and solved it in the minimal way like this.

<ScrollViewer x:Name="TS" Grid.Row="1" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" CanContentScroll="True" PanningMode="HorizontalOnly" SnapsToDevicePixels="True" Background="Transparent" PreviewMouseWheel="TS_PreviewMouseWheel">

added PreviewMouseWheel="TS_PreviewMouseWheel"

and in the code behind,

private void TS_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        TS.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
        ScrollViewer scrollviewer = sender as ScrollViewer;
        if (e.Delta > 0)
        {
            scrollviewer.LineLeft();
        }
        else
        {
            scrollviewer.LineRight();
        }
        e.Handled = true;
 }

thanks anyway.

Related Topic