When using a Popup inside a TreeView in WPF I am running into an issue where the controls inside the popup become unusable. For example, using the following code the ToggleButton inside the popup can only be toggled once and then becomes unable to be toggled back. Is there any way to work around this issue?
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TreeView>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel>
<ToggleButton>
Button outside popup
</ToggleButton>
<Popup IsOpen="True">
<ToggleButton>
Button inside popup
</ToggleButton>
</Popup>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeView>
</Grid>
</Page>
The problem is that you are embedding the Popup inside the TreeViewItem, and somehow that is interfering with its built-in functionality. I don't think Popups are meant to be used that way, anyway. Because you can specify the PlacementTarget of a Popup, you can declare them anywhere and then open them at the location you want. The following mark-up demonstrates this with your example:
<StackPanel>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=buttonPanel}"
Placement="Bottom">
<ToggleButton>
Button inside popup
</ToggleButton>
</Popup>
<TreeView>
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Name="buttonPanel">
<ToggleButton>
Button outside popup
</ToggleButton>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</TreeView>
</StackPanel>
As you can see, the Popup can be declared anywhere. You can then use PlacementTarget to put it right where you want. With this change, the ToggleButton works as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With