且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[WPF]如何限制单击Windows Forms元素时关闭弹出窗口?

更新时间:2023-12-06 11:32:58

要防止在单击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.