元素无法接收MouseUp事件

时间:2013-01-11 13:16:00

标签: c# wpf checkbox popup

CheckBox.ContentToggleButton以及 Popup 。当ToggleButtonChecked时,弹出广告会显示。在 Popup 中,有三个元素。如果我点击一个元素, Popup 应该关闭。

重现问题的步骤:

  1. 点击黑色椭圆,弹出窗口将显示
  2. 单击红色,绿色或蓝色椭圆,弹出窗口不会关闭(预计弹出窗口将关闭)
  3. 这是我的代码。

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

    和代码隐藏

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

    我发现如果我用 Border 替换CheckBox,我的代码就可以了。为什么呢?

1 个答案:

答案 0 :(得分:1)

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

据我所知,您无法在复选框中添加鼠标向上或向下事件,因为Checked事件会占用它们。由于边框没有默认行为,因此可以添加鼠标事件。您可以尝试使用PreviewMouseLeftButtonDown,它应该正常启动