本文介绍了Wpf弹出失去焦点,但保持打开状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Popup:

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

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

< StackPanel Margin =5,5,5,2
Horizo​​ntalAlignment =Stretch
Orientation =Horizo​​ntal>

< StackPanel.Resources>
< Style TargetType =Button>
< Setter Property =MarginValue =5/>
< / style>

< 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 =VerticalAlignmentValue =Center/>
< Setter Property =PaddingValue =0/>
< / style>

< Style TargetType =TextBlock>
< Setter Property =FontSizeValue =14/>
< Setter Property =VerticalAlignmentValue =Center/>
< Setter Property =PaddingValue =2/>
< / style>

< DataTemplate x:Key =TwoDistinctVersionsRangeTemplateKey>
< Grid Margin =2,4Horizo​​ntalAlignment =Stretch>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =AutoSharedSizeGroup =_ 1/>
< ColumnDefinition Width =AutoSharedSizeGroup =_ 2/>
< ColumnDefinition Width =AutoSharedSizeGroup =_ 3/>
< ColumnDefinition Width =AutoSharedSizeGroup =_ 4/>
< ColumnDefinition Width =AutoSharedSizeGroup =_ 5/>
< ColumnDefinition Width =AutoSharedSizeGroup =_ 6/>
< ColumnDefinition Width =AutoSharedSizeGroup =_ 7/>
< /Grid.ColumnDefinitions>

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

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

< TextBlock Grid.Column =2
Horizo​​ntalAlignment =Left
text ={Binding FromVersionName}
TextAlignment =Left/>

< TextBlock Grid.Column =3
Width =10
保证金=5,0
Horizo​​ntalAlignment =Center
Text = -
TextAlignment =Center/>

TextBlock Grid.Column =4
Horizo​​ntalAlignment =Left
Text ={Binding ToVersionName}
TextAlignment =Left/> ;

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

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

< DataTemplate x:Key =FixedVersionRangeTemplateKey>
< StackPanel>
TextBlock Margin =2,4
Horizo​​ntalAlignment =Left
VerticalAlignment =Center
Text ={Binding FromVersionName}
TextAlignment = 左/>
< StackPanel>
< StackPanel.Visibility>
< MultiBinding Converter ={StaticResource RangeToVisibilityConverter}>
< MultiBinding.Bindings>
< Binding />
< Binding RelativeSource ={RelativeSource Mode = FindAncestor,AncestorType = UserControl}/>
< / MultiBinding>
< RadioButton Command ={Binding RelativeSource = {RelativeSource Mode = FindAncestor,
AncestorType = UserControl},
Path = DataContext.VersionsVm.ChangeTempRangeTypeCommand}
Content =& amp ; LT; />
< RadioButton Command ={Binding RelativeSource = {RelativeSource Mode = FindAncestor,
AncestorType = UserControl},
Path = DataContext.VersionsVm.ChangeTempRangeTypeCommand}
Content =& amp ; 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 =& amp ; gt; =/>
< RadioButton Command ={Binding RelativeSource = {RelativeSource Mode = FindAncestor,
AncestorType = UserControl},
Path = DataContext.VersionsVm.ChangeTempRangeTypeCommand}
Content =& amp ; gt;中/>
< / StackPanel>
< / StackPanel>
< / DataTemplate>
< / ItemsControl>
< / StackPanel>
< / Border>
< / Popup>

我有一个很奇怪的问题。左或右聚焦离开弹出窗口,并转到父窗口。 Popus保持开放。

我不知道为什么会发生这种情况。但是我的键盘导航有问题。
我唯一可以怀疑的是Popup的父类是在WinForms的ElementHost中托管的UserControl。

我没有想法如何调试这个问题,所以我感谢任何有帮助的提示。

先谢谢你。

解决方案

在IsOpen上绑定可以覆盖StaysOpen行为。我看不到更多,但是如果这个IsOpen绑定持续从其来源的 True 值,将保持弹出打开,无论发生什么焦点。 p>

I have the following Popup:

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

I have a very strange issue with it.
When I hit Tab, Left or Right focus leaves popup and goes to the parent window. Popus stays opened.
I do not know why this happens. But I've got broken keyboard navigation.
The only thing I can suspect to be the reason is that parent of Popup is UserControl hosted inside winforms ElementHost.
I have no idea how to debug this problem so I appreciate any helpful hints.
Thank you in advance.

解决方案

Having a Binding on IsOpen can override the StaysOpen behavior. I can't tell without seeing more but if that IsOpen binding is persisting a True value from its source that would keep the Popup open no matter what happens with the focus.

这篇关于Wpf弹出失去焦点,但保持打开状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:34