1

There are:

  • A toggle button
  • A popup
  • A TreeView

the tree view is in popup,the popup is attach to the toggle button.

I binding the Popup.IsOpen to ToggleButton.IsChecked. The popup.StaysOpen=false

When I open the popup, then click outside, but the popup can't close automatically.

Even when I click the TreeView's text, then click outside, the popup can't close automatically too.

How to let the popup close when I click outside?

the mini example code is:

<Window x:Class="WpfTest1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfTest1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid>
    <ToggleButton Content="Test" Width="70" Height="30" Name="Btn" ClickMode="Press"/>
    <Popup PlacementTarget="{Binding ElementName=Btn}" StaysOpen="False" 
           IsOpen="{Binding ElementName=Btn,Path=IsChecked,Mode=TwoWay}">
        <TreeView>
            <TreeViewItem>
                <TreeViewItem Header="A"/>
                <TreeViewItem Header="B"/>
                <TreeViewItem Header="C"/>
            </TreeViewItem>
            <TreeViewItem>
                <TreeViewItem Header="D"/>
                <TreeViewItem Header="E"/>
                <TreeViewItem Header="F"/>
            </TreeViewItem>
        </TreeView>
    </Popup>
</Grid>

TownDrin
  • 69
  • 4

1 Answers1

0

This is an odd one. Apparently it's being caused by ClickMode="Press". If you take that out, the Popup opens and closes as you would expect.

I'm not sure there's any way to work around that. Either don't use ClickMode="Press", or open/close the Popup using code instead of binding.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
  • Thx,if I set the click mode to Release, When I click then toggle button again , the popup can't close, because the toggle button is clicked twice. – TownDrin Jun 16 '20 at 03:37
  • @TownDrin Hm, you're right. Take a look at this answer: https://stackoverflow.com/a/14257384/5086631 – Keith Stein Jun 16 '20 at 03:45
  • Ok, I read the answer ,it solve my question successfully. thanks a lot. – TownDrin Jun 16 '20 at 06:36