WPF PreviewMouse事件无法正常工作

时间:2018-06-05 01:54:13

标签: wpf mouseevent

使用下面的代码在附加到进程时获得一些奇怪的行为。每个按钮都有Mouseup和Mousedown。在正常运行时,只有MouseDown工作,并且跳过MouseUp。如果我在MouseUp中设置了一个断点,它仍会被跳过。在MouseDown中放置一个断点,然后两个事件都有效。任何想法或不同的方式?

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();
}

这是一个按钮的Xaml ......

<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>

1 个答案:

答案 0 :(得分:2)

如果您将Button替换为无法处理鼠标点击的其他元素,则代码可以正常运行,例如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>