user512737
user512737

Reputation:

WPF PreviewMouse events not working

Getting some strange behavior on attach to process with the code below. Each button has Mouseup and Mousedown. On a normal run only the MouseDown works and MouseUp get's skipped. If I put a break point in MouseUp, it still gets skipped. Put a breakpoint in MouseDown then both events work. Any ideas or different ways?

private void Window_Load(object sender, RoutedEventArgs e)
{
    TelescopeHardware.Start();
    TelescopeHardware.StaticPropertyChanged += PropertyChanged;

    ButtonLeft.PreviewMouseLeftButtonUp += (s, ev) => { SetPulseGuideParms(0.0, -GuideRate); };
    ButtonLeft.PreviewMouseLeftButtonDown += (s, ev) => { StartSlew(SlewDirection.SlewLeft); };

    ButtonRight.PreviewMouseLeftButtonUp += (s, ev) => { SetPulseGuideParms(0.0, GuideRate); };
    ButtonRight.PreviewMouseLeftButtonDown += (s, ev) => { StartSlew(SlewDirection.SlewRight); };

    ButtonUp.PreviewMouseLeftButtonUp += (s, ev) => { SetPulseGuideParms(GuideRate, 0.0); };
    ButtonUp.PreviewMouseLeftButtonDown += (s, ev) => { StartSlew(SlewDirection.SlewUp); };

    ButtonDown.PreviewMouseLeftButtonUp += (s, ev) => { SetPulseGuideParms(-GuideRate, 0.0); };
    ButtonDown.PreviewMouseLeftButtonDown += (s, ev) => { StartSlew(SlewDirection.SlewDown); };

    ButtonStop.PreviewMouseLeftButtonDown += (s, ev) => { TelescopeHardware.AbortSlew(); };

    SlewSpeed_Load();
    Version_Load();
    EquatorialSystem_Load();
    GetTrackingRates();
}

Here is the Xaml for one button...

<Button Grid.Column="0" Grid.Row="1" Margin="2,2,2,2">
    <Rectangle x:Name="ButtonLeft" Fill="{StaticResource Arrow-Left-Bold-Circle}" Height="32" Width="32" />
</Button>

Upvotes: 1

Views: 147

Answers (1)

mm8
mm8

Reputation: 169420

Your code will work expected if you replace the Button with another element that doesn't handle the mouse clicks, like for example a Border:

<Border Grid.Column="0" Grid.Row="1" Margin="2,2,2,2" BorderThickness="1">
    <Rectangle x:Name="ButtonLeft" Fill="{StaticResource Arrow-Left-Bold-Circle}" Height="32" Width="32" />
    <Border.Style>
        <Style TargetType="Border">
            <Setter Property="Background" Value="#FFDDDDDD"/>
            <Setter Property="BorderBrush" Value="#FF707070"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="#FFBEE6FD"/>
                    <Setter Property="BorderBrush" Value="#FF3C7FB1"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

Upvotes: 1

Related Questions