Wpf弹出失去焦点但保持打开状态

时间:2013-01-18 18:04:39

标签: wpf popup focus

我有以下弹出窗口:

<Popup Name="popupBind"
               AllowsTransparency="True"
               Helpers:FocusHelper.IsFocused="{Binding RelativeSource={RelativeSource Self},
                                                       Path=IsOpen,
                                                       Mode=OneWay}"
               HorizontalOffset="-30"
               IsOpen="{Binding IsBindingBegun,
                                Mode=TwoWay}"
               Placement="Bottom"
               PlacementTarget="{Binding ElementName=bindButton}"
               StaysOpen="False"
               >
            <Border Background="{DynamicResource {x:Static Styles:CommonStyles.ButtonsPanelBackgroundKey}}"
                    BorderBrush="Black"
                    BorderThickness="1"
                    Padding="0">
                <StackPanel HorizontalAlignment="Stretch">

                    <StackPanel.Resources>
                        <TemplateSelectors:VersionRangeDataTemplateSelector x:Key="VersionRangeDataTemplateSelector" />
                        <Converters:RangeToVisibilityConverter x:Key="RangeToVisibilityConverter" />
                    </StackPanel.Resources>

                    <StackPanel Margin="5,5,5,2"
                                HorizontalAlignment="Stretch"
                                Orientation="Horizontal">

                        <StackPanel.Resources>
                            <Style TargetType="Button">
                                <Setter Property="Margin" Value="5" />
                            </Style>
                        </StackPanel.Resources>

                        <Button Command="{Binding BindCommand}"
                               >
                        </Button>
                        <Button Command="{Binding BindCommand}"
                                >
                        </Button>
                        <Button Command="{Binding BindCommand}"
                                >
                        </Button>
                        <Button Command="{Binding BindCommand}"
                                >
                        </Button>
                    </StackPanel>

                    <ItemsControl Margin="10,2,5,5"
                                  Focusable="False"
                                  Grid.IsSharedSizeScope="True"
                                  ItemTemplateSelector="{StaticResource VersionRangeDataTemplateSelector}"
                                  ItemsSource="{Binding Path=VersionsVm.TempRanges}">
                        <ItemsControl.Resources>
                            <Style TargetType="CheckBox">
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="0" />
                            </Style>

                            <Style TargetType="TextBlock">
                                <Setter Property="FontSize" Value="14" />
                                <Setter Property="VerticalAlignment" Value="Center" />
                                <Setter Property="Padding" Value="2" />
                            </Style>

                            <DataTemplate x:Key="TwoDistinctVersionsRangeTemplateKey">
                                <Grid Margin="2,4" HorizontalAlignment="Stretch">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="_1" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="_2" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="_3" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="_4" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="_5" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="_6" />
                                        <ColumnDefinition Width="Auto" SharedSizeGroup="_7" />
                                    </Grid.ColumnDefinitions>

                                    <CheckBox Grid.Column="0" IsChecked="{Binding IncludeStartEdge, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                                    <TextBlock Grid.Column="1" Margin="2,0,5,0">
                                        <TextBlock.Style>
                                            <Style TargetType="TextBlock">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IncludeStartEdge}" Value="True">
                                                        <Setter Property="Text" Value="[" />
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IncludeStartEdge}" Value="False">
                                                        <Setter Property="Text" Value="(" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>

                                    <TextBlock Grid.Column="2"
                                               HorizontalAlignment="Left"
                                               Text="{Binding FromVersionName}"
                                               TextAlignment="Left" />

                                    <TextBlock Grid.Column="3"
                                               Width="10"
                                               Margin="5,0"
                                               HorizontalAlignment="Center"
                                               Text="-"
                                               TextAlignment="Center" />

                                    <TextBlock Grid.Column="4"
                                               HorizontalAlignment="Left"
                                               Text="{Binding ToVersionName}"
                                               TextAlignment="Left" />

                                    <TextBlock Grid.Column="5" Margin="5,0,2,0">
                                        <TextBlock.Style>
                                            <Style TargetType="TextBlock">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding IncludeEndEdge}" Value="True">
                                                        <Setter Property="Text" Value="]" />
                                                    </DataTrigger>
                                                    <DataTrigger Binding="{Binding IncludeEndEdge}" Value="False">
                                                        <Setter Property="Text" Value=")" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>

                                    <CheckBox Grid.Column="6" IsChecked="{Binding IncludeEndEdge, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                </Grid>
                            </DataTemplate>

                            <DataTemplate x:Key="FixedVersionRangeTemplateKey">
                                <StackPanel>
                                    <TextBlock Margin="2,4"
                                               HorizontalAlignment="Left"
                                               VerticalAlignment="Center"
                                               Text="{Binding FromVersionName}"
                                               TextAlignment="Left" />
                                    <StackPanel>
                                        <StackPanel.Visibility>
                                            <MultiBinding Converter="{StaticResource RangeToVisibilityConverter}">
                                                <MultiBinding.Bindings>
                                                    <Binding />
                                                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}" />
                                                </MultiBinding.Bindings>
                                            </MultiBinding>
                                        </StackPanel.Visibility>
                                        <RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                                      AncestorType=UserControl},
                                                                       Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
                                                                                                              Content="&lt;" />
                                        <RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                                      AncestorType=UserControl},
                                                                       Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"                                                         
                                                     Content="&lt;=" />
                                        <RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                                      AncestorType=UserControl},
                                                                       Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"                                                         
                                                     Content="="
                                                     IsChecked="True" />
                                        <RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                                      AncestorType=UserControl},
                                                                       Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
                                                                                                              Content="&gt;=" />
                                        <RadioButton Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                                                                                      AncestorType=UserControl},
                                                                       Path=DataContext.VersionsVm.ChangeTempRangeTypeCommand}"
                                                                                                              Content="&gt;" />
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.Resources>
                    </ItemsControl>
                </StackPanel>
            </Border>
        </Popup>

我有一个非常奇怪的问题 当我点击Tab时,左或右焦点会弹出并转到父窗口。 Popus保持开放状态 我不知道为什么会这样。但是我的键盘导航已经坏了 我唯一可以怀疑的是Popup的父级是在winforms ElementHost中托管的UserControl。
我不知道如何调试这个问题,所以我感谢任何有用的提示 提前谢谢。

1 个答案:

答案 0 :(得分:3)

在IsOpen上绑定可以覆盖StaysOpen行为。如果没有看到更多,我无法分辨,但是如果IsOpen绑定从其源代码持久保存True值,无论焦点发生什么,都会使Popup保持打开状态。