WPF嵌套弹出窗口未关闭

umuewwlo  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(145)

我有一个带有Image的窗口,它显示了来自MouseEnter事件处理程序的Popup:

private void theImage_MouseEnter(object sender, MouseEventArgs e)
    {
        contolPopup.IsOpen = true;
    }

此contolPopup中有一个按钮,它显示来自其Click事件处理程序的另一个Popup:

private void setButton_Click(object sender, RoutedEventArgs e)
    {
        paramPopup.IsOpen = true;
    }

这个paramPopup在显示后的行为有点奇怪:如果我点击它外面的某个地方,它不会关闭,但是如果我点击这个paramPopup里面的某个控件,然后点击外面的某个地方,它会很好地关闭。
是什么原因导致这种行为?
我尝试聚焦paramPopup,但这没有帮助:

private void setButton_Click(object sender, RoutedEventArgs e)
    {
        paramPopup.IsOpen = setButton.IsChecked??false;

        paramPopup.Focus();
    }

两个弹出窗口的Popup.StaysOpen都设置为false。我使用.NET4.0
下面我提供了完整的源代码:
从窗口打开第一个弹出窗口:

<Window x:Class="GeoControls.WebcamWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:GeoControls"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        SizeChanged="Window_SizeChanged" Loaded="Window_Loaded" SizeToContent="WidthAndHeight" ResizeMode="NoResize"
        Title="{Binding Name}"
        MouseEnter="theImage_MouseEnter" MouseLeave="theImage_MouseLeave" MouseLeftButtonUp="Window_MouseLeftButtonUp" Closed="Window_Closed">
    <Grid Background="Black">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Image Grid.Row="0" Grid.ColumnSpan="2" Name="theImage" Width="{Binding CurrentResolution.Width}" Height="{Binding CurrentResolution.Height}" />
        <TextBlock Name="waitingTextBlock" Grid.Row="0" Text="Waiting the response from TV Server..." Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>
        <Popup Name="contolPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=theImage}" PopupAnimation="Scroll" Placement="Left" AllowsTransparency="True">
            <Border CornerRadius="10" BorderThickness="1" BorderBrush="Black" Padding="10" Background="White">
                <local:WebcamControl x:Name="webcamControl" Background="White">
                </local:WebcamControl>
            </Border>
        </Popup>
    </Grid>
</Window>

第二个弹出窗口由包含在第一个弹出窗口中的WebcamControl中的ParamButtons打开:

<UserControl x:Class="GeoControls.ParamButtons"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xmlns:local="clr-namespace:GeoControls"
             d:DesignHeight="30" d:DesignWidth="300">
    <Grid>

        ............ omitted some code here................                

        <Popup Name="paramPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=setButton}" AllowsTransparency="True" Closed="paramPopup_Closed">
            <Border Focusable="True" CornerRadius="10" BorderThickness="1" BorderBrush="Black" Padding="10" Background="White">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="300"/>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="50"/>
                    </Grid.ColumnDefinitions>

                    <local:ParamSlider Grid.Column="0" x:Name="paramSlider" />
                    <Button Content="OK" Click="okButton_Click" Grid.Column="1" Margin="5" />
                    <Button Content="Cancel" Click="cancelButton_Click" Grid.Column="2" Margin="5" />
                </Grid>
            </Border>
        </Popup>
    </Grid>
</UserControl>
dgsult0t

dgsult0t1#

这段代码应该有助于解决这个问题:

private void paramPopup_Closed(object sender, RoutedEventArgs e)
{
    Mouse.Capture(null);
}

在此回复https://stackoverflow.com/a/33974360/16286963中有更详细的解释

相关问题