[WPF]如何限制单击Windows Forms元素时关闭弹出窗口? [英] [WPF] How to restrict the popup from closing while click on a Windows Forms element?

查看:62
本文介绍了[WPF]如何限制单击Windows Forms元素时关闭弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在弹出窗口中使用WinForms和WPF按钮.我希望StaysOpen为假.

I am using WinForms and WPF button inside a Popup. I want StaysOpen to be false.

单击WinForms按钮将关闭弹出窗口,而使用WPF按钮则不会.

Clicking WinForms button closes the popup whereas it does not happens with WPF button.

是否可以限制此行为?我希望在弹出窗口外面单击时将其关闭.

Is it possible to restrict this behavior? I want the popup to close when I click outside it.

<StackPanel  >
            <Button Content="Open popup" Name="buttonpopup" Click="Button_Click" HorizontalAlignment="Center" Margin="0 20 0 0"/>
            <!--Adding WFH in the Dock Panel-->
            <Popup StaysOpen="False" Focusable="False" PlacementTarget="{Binding ElementName=buttonpopup}" Placement="Bottom" Name="pop" IsOpen="True" VerticalOffset="20">
                <Border BorderBrush="Black" BorderThickness="1" >
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Button Content="Click WPF button" Margin="10"/>
                        <wfi:WindowsFormsHost Height="Auto" Focusable="False" Width="Auto" Grid.Row="1">
                            <wf:Button x:Name="Button" Text="Click WF Button" />
                        </wfi:WindowsFormsHost>
                    </Grid>
                </Border>
            </Popup>
        </StackPanel>

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

致谢,

杰西

推荐答案

要防止在单击WindowsFormsHost中的控件时关闭弹出窗口,可以简单地设置弹出窗口的Background属性.包括透明"的任何画笔的根元素:

To prevent the Popup from being closed when you click on the control in the WindowsFormsHost you could simply set the Background property of the Popup's root element to any Brush including Transparent:

<StackPanel  >
            <Button Content="Open popup" Name="buttonpopup" Click="Button_Click" HorizontalAlignment="Center" Margin="0 20 0 0"/>
            <!--Adding WFH in the Dock Panel-->
            <Popup StaysOpen="False" PlacementTarget="{Binding ElementName=buttonpopup}" 
                   Placement="Bottom" Name="pop" IsOpen="True" VerticalOffset="20">
                <Border BorderBrush="Black" BorderThickness="1" >
                    <Grid Background="White">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Button Content="Click WPF button" Margin="10"/>
                        <WindowsFormsHost Height="Auto" Width="Auto" Grid.Row="1">
                            <wf:Button x:Name="Button" Text="Click WF Button" />
                        </WindowsFormsHost>
                    </Grid>
                </Border>
            </Popup>
        </StackPanel>

如果您未指定背景,则弹出区域被视为属于父窗口,这就是弹出窗口关闭的原因.

If you don't specify a Background the popup area is considered to belong to the parent window and that's why the Popup closes.

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于[WPF]如何限制单击Windows Forms元素时关闭弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆