1

当我使用弹出窗口时,它似乎在附近徘徊。在下面的代码中,我通过覆盖控件模板将弹出窗口附加到文本框,并在文本框具有焦点时显示弹出窗口。当您切换到屏幕上的下一个元素时,弹出窗口会消失,但如果您只是 alt-tab 切换到不同的应用程序,弹出窗口会停留在前台。我该如何摆脱它?

<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>
4

4 回答 4

3

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

如果StaysOpenis True,这是默认设置,它将保持打开状态,直到控件不再处于焦点。如果是False,它将保持打开状态,直到在控件之外发生鼠标或键盘事件Popup,这可能是 alt-tabing 时的情况。您可能需要对其进行一些调整以使其行为符合您的要求,但这可能是一个起点。

于 2009-12-09T04:51:02.057 回答
2

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

于 2011-01-05T17:54:31.410 回答
0

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

检查这个:

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

希望这对你有帮助!!

于 2009-12-09T05:30:18.920 回答
0

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

于 2009-12-09T04:45:29.617 回答