我该如何让那个该死的 wpf 弹出窗口消失?

发布于 2024-08-14 05:27:18 字数 3574 浏览 5 评论 0原文

当我使用弹出窗口时,它似乎一直在徘徊。在下面的代码中,我通过覆盖控件模板将弹出窗口附加到文本框,并使弹出窗口在文本框具有焦点时出现。当您按 Tab 键切换到屏幕上的下一个元素时,弹出窗口会消失,但如果您只是按 Alt 键切换到其他应用程序,则弹出窗口会保留在前台。我该如何摆脱它?

<Window x:Class="DropDownPicker.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
      <StackPanel>
         <TextBox Text="hello">
         <TextBox.Style>
            <!-- Simple TextBox -->
            <Style
               TargetType="{x:Type TextBox}">
               <Setter
                  Property="KeyboardNavigation.TabNavigation"
                  Value="None" />
               <Setter
                  Property="FocusVisualStyle"
                  Value="{x:Null}" />
               <Setter
                  Property="AllowDrop"
                  Value="true" />
               <Setter
                  Property="Template">
                  <Setter.Value>
                     <ControlTemplate
                        TargetType="{x:Type TextBox}">
                        <Grid>
                           <Border
                              x:Name="Border"
                              Background="{DynamicResource WindowBackgroundBrush}"
                              BorderBrush="{DynamicResource SolidBorderBrush}"
                              BorderThickness="1"
                              Padding="2"
                              CornerRadius="2">

                              <Grid>
                              <!-- The implementation places the Content into the ScrollViewer. It must be named PART_ContentHost for the control to function -->
                              <ScrollViewer
                                 Margin="0"
                                 x:Name="PART_ContentHost"
                                 Style="{DynamicResource SimpleScrollViewer}"
                                 Background="{TemplateBinding Background}" />

                              <Popup
                                 x:Name="thePopup"
                                 IsOpen="False">
                                 <Border
                                    BorderBrush="Red"
                                    BorderThickness="5">
                                    <TextBlock
                                       Text="Hellssss" />
                                 </Border>
                              </Popup>
                              </Grid>
                           </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                              <Trigger
                                 Property="IsFocused"
                                 Value="True">
                                 <Setter
                                    TargetName="thePopup"
                                    Property="IsOpen"
                                    Value="True" />
                              </Trigger>
                        </ControlTemplate.Triggers>
                     </ControlTemplate>
                  </Setter.Value>
               </Setter>
            </Style>
         </TextBox.Style>
      </TextBox>
         <TextBox
            Text="ssss" />
       </StackPanel>
    </Grid>
</Window>

when i use a popup, it seems to hang around. in the code below i attach a popup to a textBox using by overriding the control template, and make the popup appear when the TextBox has focus. When you tab to the next on screen element the popup goes away, but if you just alt-tab to a different application the popup stays there in the foreground. how do i get rid of it?

<Window x:Class="DropDownPicker.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
      <StackPanel>
         <TextBox Text="hello">
         <TextBox.Style>
            <!-- Simple TextBox -->
            <Style
               TargetType="{x:Type TextBox}">
               <Setter
                  Property="KeyboardNavigation.TabNavigation"
                  Value="None" />
               <Setter
                  Property="FocusVisualStyle"
                  Value="{x:Null}" />
               <Setter
                  Property="AllowDrop"
                  Value="true" />
               <Setter
                  Property="Template">
                  <Setter.Value>
                     <ControlTemplate
                        TargetType="{x:Type TextBox}">
                        <Grid>
                           <Border
                              x:Name="Border"
                              Background="{DynamicResource WindowBackgroundBrush}"
                              BorderBrush="{DynamicResource SolidBorderBrush}"
                              BorderThickness="1"
                              Padding="2"
                              CornerRadius="2">

                              <Grid>
                              <!-- The implementation places the Content into the ScrollViewer. It must be named PART_ContentHost for the control to function -->
                              <ScrollViewer
                                 Margin="0"
                                 x:Name="PART_ContentHost"
                                 Style="{DynamicResource SimpleScrollViewer}"
                                 Background="{TemplateBinding Background}" />

                              <Popup
                                 x:Name="thePopup"
                                 IsOpen="False">
                                 <Border
                                    BorderBrush="Red"
                                    BorderThickness="5">
                                    <TextBlock
                                       Text="Hellssss" />
                                 </Border>
                              </Popup>
                              </Grid>
                           </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                              <Trigger
                                 Property="IsFocused"
                                 Value="True">
                                 <Setter
                                    TargetName="thePopup"
                                    Property="IsOpen"
                                    Value="True" />
                              </Trigger>
                        </ControlTemplate.Triggers>
                     </ControlTemplate>
                  </Setter.Value>
               </Setter>
            </Style>
         </TextBox.Style>
      </TextBox>
         <TextBox
            Text="ssss" />
       </StackPanel>
    </Grid>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云之铃。 2024-08-21 05:27:18

您是否尝试过将 StaysOpen 属性设置为 False

如果 StaysOpenTrue(默认值),它将保持打开状态,直到控件不再获得焦点。如果为False,它将保持打开状态,直到在Popup 控件外部发生鼠标或键盘事件,这可能是在使用 alt-tab 键时出现的情况。您可能需要对其进行一些调整才能使其表现得像您想要的那样,但这可能是一个起点。

Have you tried setting the StaysOpen property to False?

If StaysOpen is True, which is the default, it will stay open until the control is no longer in focus. If it is False it will stay open until a mouse or keyboard event occurs outside of the Popup control, which may be the case while alt-tabing. You might have to tweak it a bit to get it to behave like you want, but it may be a starting point.

时光匆匆的小流年 2024-08-21 05:27:18

我监听了 LostMouseCapture 事件,然后将 Popup 上的 StaysOpen 属性设置为 false

I listened to the LostMouseCapture event and then set the StaysOpen property on the Popup to false

塔塔猫 2024-08-21 05:27:18

这是设计使然;窗口焦点!= 控件焦点,否则当您离开窗口并返回时,光标将跳回到第一个控件。如果您希望在窗口不活动时隐藏弹出窗口,则必须手动执行此操作。

This is by-design; Window focus != Control focus, otherwise when you tabbed away from a window and came back, your cursor would jump back to the first control. If you want the pop up to be hidden when the window isn't active, you have to manually do this.

抠脚大汉 2024-08-21 05:27:18

这里也提出了类似的问题:
WPF 弹出 ZOrder

检查此:

http://chriscavanagh.wordpress.com/2008/08/13/non-topmost-wpf-popup/< /a>

希望这对你有帮助!

Similar question is asked here also:
WPF Popup ZOrder

Check this:

http://chriscavanagh.wordpress.com/2008/08/13/non-topmost-wpf-popup/

Hope this help you!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文