0

I have a StackPanel with two textblocks. When the StackPanel is clicked, in the code behind I set a Popup to IsOpen=True. I would like this Popup to go away when the user clicks anywhere else in the app that is outside of the Popup.

<DataTemplate DataType="{x:Type something}">
    <StackPanel Orientation="Vertical" Margin="3" MouseUp="PanelClick">
        <Popup AllowsTransparency="True" PopupAnimation="Slide"
                StaysOpen="False">
            <Border
                    Background="{DynamicResource   {x:Static SystemColors.ControlBrushKey}}"
                    Padding="4" Opacity="1" CornerRadius="3"
                    MinHeight="24" MaxWidth="267">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="Something"/>
                    <TextBox>aaa</TextBox>
                    <TextBox>bbb</TextBox>
                </StackPanel>
            </Border>
        </Popup>
        <TextBlock
                Text="Something else" />
        <TextBlock
                Text="Some other stuff" />
    </StackPanel>
</DataTemplate>

In the code behind I have

private void PanelClick(object sender, MouseButtonEventArgs e)
{
    var panel = sender as StackPanel;
    var pop = panel?.Children.Cast<Popup>().FirstOrDefault();
    if (pop != null)
    {
        pop.IsOpen = true;
    }
}

With the code as presented, I find that the Popup does NOT go away by itself unless I first click (to give focus) into the aaa or bbb textbox and THEN click away from the pop up. It also gives away if I click on a different app. This seems in contradiction to the StaysOpen documentation [https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.staysopen%28v=vs.110%29.aspx]

However, if I put a button around my StackPanel and do the code-behind from the Click event then the Popup works as stated in the documentation

<DataTemplate DataType="{x:Type something}">
    <Button Click="ButtonDo">
        <StackPanel Orientation="Vertical" Margin="3">
            <Popup AllowsTransparency="True" PopupAnimation="Slide"
                    StaysOpen="False">
                <Border
                        Background="{DynamicResource   {x:Static SystemColors.ControlBrushKey}}"
                        Padding="4" Opacity="1" CornerRadius="3"
                        MinHeight="24" MaxWidth="267">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="Something"/>
                        <TextBox>moo</TextBox>
                        <TextBox>moo</TextBox>
                    </StackPanel>
                </Border>
            </Popup>
            <TextBlock
                    Text="Something else" />
            <TextBlock
                    Text="Some other stuff" />
        </StackPanel>
    </Button>
</DataTemplate>

Why is there this difference of behaviour? Also, as a curiosity, attempting to use codebehind with MouseUp from the button doesn't even call the code behind - but that's a problem I don't need to know why.

Peter pete
  • 704
  • 1
  • 7
  • 17
  • Use `ToggleButton` and bind `Popup.IsOpen` to `ToggleButton.IsChecked`. Currently your code behind seems to missing part to give focus to popup when it's opened, see [this](http://stackoverflow.com/q/14252180/1997232). – Sinatr Jun 07 '16 at 11:36
  • Correct, I'm not giving it focus. Why do I have to give it focus? The button, when the Click is done, makes the Popup work correctly. I had previously bound a ToggleButton Checked and Uncheck event to code behind and shown and hid the box that way, but then that reqd the user to tap on the toggle button to make it go away. I then tried the IsOpen with some LostFocus and that didn't work either. – Peter pete Jun 07 '16 at 12:32

0 Answers0