我有一个弹出窗口,当我单击ToggleButton时会打开。我希望弹出窗口在用户单击外部时关闭,因此我将其StaysOpen属性设置为false。这在大多数情况下都有效。
我的问题是,我的弹出窗口包含一个组合框。很多时候,有很多选择,导致它超过了Popup区域。当我选择外部的任何选项时,也会导致弹出窗口也关闭。
我唯一想到的解决方案是将StaysOpen设置为True并自己处理外部点击(尽管我也没有想过如何知道点击是否确实在无关控件上)。真的没有简单的方法可以解决此问题吗?
注意:我无法添加关闭按钮并更改设计。
您将需要提供代码,因为它看起来像是开箱即用的那样。这是我的测试用例(纯xaml,无代码隐藏),即使在Popup
:
StaysOpen="False"
区域外的ComboBox项而无需关闭它<Window x:Class="PopupComboboxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<ToggleButton x:Name="toggleButton" Content="Toggle Button For Popup" HorizontalAlignment="Left" Height="50" Width="150"/>
<Popup Name="myPopup" StaysOpen="False" Placement="MousePoint">
<Popup.Style>
<Style TargetType="Popup">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=toggleButton,Path=IsChecked}" Value="True">
<Setter Property="IsOpen" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
<Grid Background="Purple">
<StackPanel>
<TextBlock Foreground="White" Text="Popup" Margin="5"/>
<ComboBox Margin="25" Width="100" Background="Blue">
<ComboBox.Items>
<ComboBoxItem>Item 1</ComboBoxItem>
<ComboBoxItem>Item 2</ComboBoxItem>
<ComboBoxItem>Item 3</ComboBoxItem>
<ComboBoxItem>Item 4</ComboBoxItem>
<ComboBoxItem>Item 5</ComboBoxItem>
<ComboBoxItem>Item 6</ComboBoxItem>
<ComboBoxItem>Item 7</ComboBoxItem>
<ComboBoxItem>Item 8</ComboBoxItem>
<ComboBoxItem>Item 9</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
</StackPanel>
</Grid>
</Popup>
</StackPanel>
</Grid>
</Window>