C# 元素无法接收MouseUp事件

C# 元素无法接收MouseUp事件,c#,wpf,checkbox,popup,C#,Wpf,Checkbox,Popup,复选框。内容是一个切换按钮以及一个弹出窗口。选中切换按钮时,将显示弹出窗口。在弹出窗口中,有三个元素。如果单击一个元素,弹出窗口应关闭 重现问题的步骤: 单击黑色椭圆,弹出窗口将显示 单击红色、绿色或蓝色椭圆,弹出窗口不会关闭(弹出窗口预计将关闭) 这是我的密码 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentati

复选框。内容
是一个
切换按钮
以及一个
弹出窗口
。选中
切换按钮
时,将显示弹出窗口。在
弹出窗口中
,有三个元素。如果单击一个元素,
弹出窗口应关闭

重现问题的步骤:

  • 单击黑色椭圆,弹出窗口将显示
  • 单击红色、绿色或蓝色椭圆,弹出窗口不会关闭(弹出窗口预计将关闭)
  • 这是我的密码

    <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>
    
    我发现如果我用边框替换
    复选框
    ,我的代码就可以工作了。为什么?

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

    据我所知,您不能将鼠标向上或向下事件添加到复选框中,因为它们被选中的事件使用。由于border没有默认行为,因此可以添加鼠标事件。您可以尝试使用PreviewMouseLeftButtonDown
    ,它应该可以正常启动

    谢谢。我几乎忘了预演。我注意到,如果在PreviewMouseLeftButtonDown中设置e.Handled=true,复选框将永远不会切换。这就是我想要的!
    I find that if I substitute CheckBox by Border, my code will work. Why?