简体   繁体   English

元素无法接收MouseUp事件

[英]Elements cannot receive MouseUp event

CheckBox.Content is a ToggleButton along with a Popup . CheckBox.Content是一个ToggleButton和一个Popup When the ToggleButton is Checked , Popup shows. Checked ToggleButton ,将显示弹出窗口 In Popup , there are three elements. Popup ,包含三个元素。 If I click one element, the Popup should close. 如果单击一个元素,则Popup应关闭。

Steps to reproduce the issue: 重现此问题的步骤:

  1. Click black ellipse, the popup will show 单击黑色椭圆,将显示弹出窗口
  2. Click red, green or blue ellipse, the popup doesn't close (the popup is expected to be closed) . 单击红色,绿色或蓝色的椭圆,弹出窗口不会关闭(预计该弹出窗口将关闭)

This is my code. 这是我的代码。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <CheckBox>
            <DockPanel Panel.ZIndex="2"  HorizontalAlignment="Right">
                <Popup Name="facePopup" DockPanel.Dock="Bottom" StaysOpen="True"  AllowsTransparency="True"
                       Placement="Bottom" VerticalOffset="1"
                       IsOpen="{Binding Path=IsChecked, ElementName=roleFaceButton}" MouseUp="facePopup_MouseUp" >
                    <StackPanel>
                        <Ellipse Fill="Red" Width="18" Height="25"/>
                        <Ellipse Fill="Green" Width="18" Height="25"/>
                        <Ellipse Fill="Blue" Width="18" Height="25"/>
                    </StackPanel>
                </Popup>
                <ToggleButton x:Name="roleFaceButton">
                    <Ellipse Fill="Black" Width="18" Height="25"/>
                </ToggleButton>
            </DockPanel>
        </CheckBox>
</Window>

and code-behind 和代码隐藏

private void facePopup_MouseUp(object sender, MouseButtonEventArgs e)
{
    facePopup.IsOpen = false;
}

I find that if I substitute CheckBox by Border , my code will work. 我发现如果我用Border代替CheckBox ,那么我的代码将起作用。 Why? 为什么?

I find that if I substitute CheckBox by Border, my code will work. Why?

To my knowledge you cannot add mouse up or down events to the checkbox as they are consumed by the Checked event. 据我所知,您不能将鼠标向上或向下事件添加到复选框,因为它们已由Checked事件占用。 As border does not have that default behaviour it's fine to add mouse events. 由于border没有默认行为,因此可以添加鼠标事件。 you could try using PreviewMouseLeftButtonDown and it should fire properly 您可以尝试使用PreviewMouseLeftButtonDown ,它应该会正确触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM