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>