user746461
user746461

Reputation:

Elements cannot receive MouseUp event

CheckBox.Content is a ToggleButton along with a Popup. When the ToggleButton is Checked, Popup shows. In Popup, there are three elements. If I click one element, the Popup should close.

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. Why?

Upvotes: 0

Views: 435

Answers (1)

Adam Bilinski
Adam Bilinski

Reputation: 1198

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. As border does not have that default behaviour it's fine to add mouse events. you could try using PreviewMouseLeftButtonDown and it should fire properly

Upvotes: 1

Related Questions