views:

533

answers:

3

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>
A: 

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.

Paul Betts
the wpf combobox does it, ive had a crawl thru reflector but couldnt find it, any idea how?
Aran Mulholland
So I'm not saying it's impossible, only that this behavior must be explicitly coded for - try expanding the ComboBox template using Blend
Paul Betts
A: 

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.

Michael Morton
if i set StaysOpen in the above code to false, my popup never appears.
Aran Mulholland
A: 

Similar question is asked here also: http://stackoverflow.com/questions/1267349/wpf-popup-zorder/1682600#1682600

Check this:

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

Hope this help you!!

viky