【问题标题】:Wpf Popup loses focus but stays openedWpf Popup 失去焦点但保持打开状态
【发布时间】:2013-01-18 18:04:38
【问题描述】:

我有以下弹出窗口:

<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。
我不知道如何调试这个问题,所以我很感激任何有用的提示。
提前谢谢你。

【问题讨论】:

  • 什么设置 IsBindingBegun 属性?
  • 这不是绑定 =) 这是实体的绑定 - 使它们相关。除了名称之外,与 WPF 绑定没有什么共同点。

标签: wpf popup focus


【解决方案1】:

对 IsOpen 进行绑定可以覆盖 StaysOpen 行为。如果没有看到更多信息,我无法判断,但如果 IsOpen 绑定从其源中持续存在 True 值,那么无论焦点发生什么情况,都会使 Popup 保持打开状态。

【讨论】:

  • 是的,我猜是 IsOpen 的绑定。但我不知道为什么键盘导航不起作用。
  • 再说一次,我不知道其余代码发生了什么,但我怀疑 FocusHelper.IsFocused 绑定也可能会干扰您的焦点,因为它会一直保持 True随着弹出窗口的显示,可能会阻止包含的控件获得焦点。
猜你喜欢
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多