WPF Click Button Scrollviewer
<p>i have the Problem that i have a Menu inside a Stackpanel, that is inside of an ScrollViewer. Now it's possible to scroll horizontally even if the Horizontally Scrollbar is Hidden. The Mainproblem now is, that i can't make a Clickevent for the buttons. It seems that my Scroll Event blocks something under it.</p>

<p><a href="https://i.stack.imgur.com/9H8g6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9H8g6.png" alt="enter image description here"></a></p>

<p>The XAML Code</p>

<pre><code>&lt;Grid&gt;
    &lt;Grid.ColumnDefinitions&gt;
        &lt;ColumnDefinition Width="200"/&gt;
        &lt;ColumnDefinition Width="*"/&gt;
        &lt;ColumnDefinition Width="200"/&gt;
    &lt;/Grid.ColumnDefinitions&gt;
    &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition Height="110"/&gt;
        &lt;RowDefinition Height="*"/&gt;
        &lt;RowDefinition Height="100"/&gt;
    &lt;/Grid.RowDefinitions&gt;
    &lt;Rectangle x:Name="rectangel1" Grid.RowSpan="3"&gt;
        &lt;Rectangle.Fill&gt;
            &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                &lt;GradientStop Color="#FF0036A0" Offset="0.003"/&gt;
                &lt;GradientStop Color="#FFE9EDFF" Offset="1"/&gt;
            &lt;/LinearGradientBrush&gt;
        &lt;/Rectangle.Fill&gt;
    &lt;/Rectangle&gt;
    &lt;Grid Grid.Column="1" Grid.ColumnSpan="2"&gt;
        &lt;Grid.ColumnDefinitions&gt;
            &lt;ColumnDefinition Width="20"/&gt;
            &lt;ColumnDefinition Width="750"/&gt;
            &lt;ColumnDefinition Width="20"/&gt;
        &lt;/Grid.ColumnDefinitions&gt;
        &lt;ScrollViewer x:Name="scrollviewer1" Grid.Column="1" Grid.ColumnSpan="2" 
            HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled"
            PanningMode="HorizontalOnly"
            MouseUp="UIElement_OnMouseUp"
            PreviewMouseLeftButtonDown="UIElement_OnPreviewMouseLeftButtonDown"
            PreviewMouseMove="UIElement_OnPreviewMouseMove" Cursor="Hand"&gt;

            &lt;StackPanel Orientation="Horizontal" Grid.Column="1" Grid.ColumnSpan="1"&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn1" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="No Btn 1" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn2" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="No Btn 2" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;
                &lt;!--&lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn3" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="Station 3" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;--&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Button x:Name="btn3" Content="Station 3" Click="btn3_Click" Style="{DynamicResource ButtonStyle1}" &gt;
                        &lt;Button.Resources&gt;
                            &lt;Style x:Key="ButtonFocusVisual"&gt;
                                &lt;Setter Property="Control.Template"&gt;
                                    &lt;Setter.Value&gt;
                                        &lt;ControlTemplate&gt;
                                            &lt;Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/&gt;
                                        &lt;/ControlTemplate&gt;
                                    &lt;/Setter.Value&gt;
                                &lt;/Setter&gt;
                            &lt;/Style&gt;
                            &lt;LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"&gt;
                                &lt;GradientStop Color="#F3F3F3" Offset="0"/&gt;
                                &lt;GradientStop Color="#EBEBEB" Offset="0.5"/&gt;
                                &lt;GradientStop Color="#DDDDDD" Offset="0.5"/&gt;
                                &lt;GradientStop Color="#CDCDCD" Offset="1"/&gt;
                            &lt;/LinearGradientBrush&gt;
                            &lt;SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/&gt;
                            &lt;Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"&gt;
                                &lt;Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/&gt;
                                &lt;Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/&gt;
                                &lt;Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/&gt;
                                &lt;Setter Property="BorderThickness" Value="1"/&gt;
                                &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
                                &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
                                &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
                                &lt;Setter Property="Padding" Value="1"/&gt;
                                &lt;Setter Property="Template"&gt;
                                    &lt;Setter.Value&gt;
                                        &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                                            &lt;Grid&gt;
                                                &lt;Rectangle Fill="#FFFFA6A6" RadiusY="10" RadiusX="10"/&gt;
                                                &lt;Label x:Name="label" Content="BUTTON" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                                            &lt;/Grid&gt;
                                            &lt;ControlTemplate.Triggers&gt;
                                                &lt;Trigger Property="IsKeyboardFocused" Value="true"/&gt;
                                                &lt;Trigger Property="ToggleButton.IsChecked" Value="true"/&gt;
                                                &lt;Trigger Property="IsEnabled" Value="false"&gt;
                                                    &lt;Setter Property="Foreground" Value="#ADADAD"/&gt;
                                                &lt;/Trigger&gt;
                                            &lt;/ControlTemplate.Triggers&gt;
                                        &lt;/ControlTemplate&gt;
                                    &lt;/Setter.Value&gt;
                                &lt;/Setter&gt;
                            &lt;/Style&gt;
                        &lt;/Button.Resources&gt;
                    &lt;/Button&gt;
                &lt;/Grid&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn4" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="No Btn 4" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn5" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="No Btn 5" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn6" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="No Btn 6" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn7" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="No Btn 7" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,10,10"&gt;
                    &lt;Rectangle x:Name="btn8" Fill="#FFF39999" RadiusX="10" RadiusY="10" /&gt;
                    &lt;Label Content="No Btn 8" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                &lt;/Grid&gt;
                &lt;Grid Width="87" Height="90" Margin="10,10,50,10"&gt;
                    &lt;Button x:Name="btn9" Content="Station 9" Click="btn9_Click" Style="{DynamicResource ButtonStyle1}" &gt;
                        &lt;Button.Resources&gt;
                            &lt;Style x:Key="ButtonFocusVisual"&gt;
                                &lt;Setter Property="Control.Template"&gt;
                                    &lt;Setter.Value&gt;
                                        &lt;ControlTemplate&gt;
                                            &lt;Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/&gt;
                                        &lt;/ControlTemplate&gt;
                                    &lt;/Setter.Value&gt;
                                &lt;/Setter&gt;
                            &lt;/Style&gt;
                            &lt;LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0"&gt;
                                &lt;GradientStop Color="#F3F3F3" Offset="0"/&gt;
                                &lt;GradientStop Color="#EBEBEB" Offset="0.5"/&gt;
                                &lt;GradientStop Color="#DDDDDD" Offset="0.5"/&gt;
                                &lt;GradientStop Color="#CDCDCD" Offset="1"/&gt;
                            &lt;/LinearGradientBrush&gt;
                            &lt;SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/&gt;
                            &lt;Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"&gt;
                                &lt;Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/&gt;
                                &lt;Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/&gt;
                                &lt;Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/&gt;
                                &lt;Setter Property="BorderThickness" Value="1"/&gt;
                                &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt;
                                &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt;
                                &lt;Setter Property="VerticalContentAlignment" Value="Center"/&gt;
                                &lt;Setter Property="Padding" Value="1"/&gt;
                                &lt;Setter Property="Template"&gt;
                                    &lt;Setter.Value&gt;
                                        &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                                            &lt;Grid&gt;
                                                &lt;Rectangle Fill="#FFFFA6A6" RadiusY="10" RadiusX="10"/&gt;
                                                &lt;Label x:Name="label" Content="Button 9" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontSize="16" FontWeight="Bold"/&gt;
                                            &lt;/Grid&gt;
                                            &lt;ControlTemplate.Triggers&gt;
                                                &lt;Trigger Property="IsKeyboardFocused" Value="true"/&gt;
                                                &lt;Trigger Property="ToggleButton.IsChecked" Value="true"/&gt;
                                                &lt;Trigger Property="IsEnabled" Value="false"&gt;
                                                    &lt;Setter Property="Foreground" Value="#ADADAD"/&gt;
                                                &lt;/Trigger&gt;
                                            &lt;/ControlTemplate.Triggers&gt;
                                        &lt;/ControlTemplate&gt;
                                    &lt;/Setter.Value&gt;
                                &lt;/Setter&gt;
                            &lt;/Style&gt;
                        &lt;/Button.Resources&gt;
                    &lt;/Button&gt;
                &lt;/Grid&gt;
            &lt;/StackPanel&gt;

        &lt;/ScrollViewer&gt;

        &lt;Rectangle x:Name="rectlinks" Grid.Column="0" Fill="#FFFF7676" MouseEnter="rectlinks_MouseEnter" /&gt;
        &lt;Rectangle x:Name="rectrechts" Grid.Column="2" MouseEnter="rectrechts_MouseEnter" Fill="#FFFF7474"  /&gt;
    &lt;/Grid&gt;
    &lt;Label x:Name="label1" Content="Wert"/&gt;
    &lt;Label x:Name="label2" Content="mehr" Margin="0,50,0,0"/&gt;


&lt;/Grid&gt;
</code></pre>

<p>The C# Code</p>

<pre><code>using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace ScrollMenue
{
    /// &lt;summary&gt;
    /// Interaktionslogik für MainWindow.xaml
    /// &lt;/summary&gt;
    public partial class MainWindow : Window
    {
        private double hOff;

        public MainWindow()
        {
            InitializeComponent();



        }



        private void rectrechts_MouseEnter(object sender, MouseEventArgs e)
        {
            label1.Content = "rechts";
            scrollviewer1.LineRight();
        }

        private void rectlinks_MouseEnter(object sender, MouseEventArgs e)
        {

            if (rectlinks.IsMouseOver == true)
            {
                scrollviewer1.ScrollToHorizontalOffset(scrollviewer1.HorizontalOffset + 10);
                label1.Content = "links";
                scrollviewer1.LineLeft();
            }
        }


        private Point scrollMousePoint;

        private void UIElement_OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            scrollviewer1.ReleaseMouseCapture();
        }

        private void UIElement_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            scrollviewer1.CaptureMouse();
            scrollMousePoint = e.GetPosition(scrollviewer1);
            hOff = scrollviewer1.HorizontalOffset;
        }

        private void UIElement_OnPreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (scrollviewer1.IsMouseCaptured)
            {
                scrollviewer1.ScrollToHorizontalOffset(hOff + (scrollMousePoint.X - e.GetPosition(scrollviewer1).X));
            }
        }

        private void btn9_Click(object sender, RoutedEventArgs e)
        {
            label2.Content = "Click";
        }

        private void btn3_Click(object sender, RoutedEventArgs e)
        {
            label2.Content = "Click";
        }
    }
}
</code></pre>