WPF-单击外部时弹出窗口未关闭

时间:2019-04-17 10:15:08

标签: wpf popup focus

我有Custom控件,它使用控件模板,如下面的代码块所示。当用户左键单击控件时,我需要显示弹出窗口。一旦他/她在弹出窗口之外单击,它将关闭。
使用MouseLeftButtonUp,将显示弹出窗口,但是在弹出窗口之外单击时保持打开状态。
请注意,一旦使用behaviours:Focus

打开弹出窗口,我便将重点放在弹出列表框元素上
<ControlTemplate x:Key="ControlWithPopup" TargetType="local:CustomizedControl">
                <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonUp">
                            <ei:ChangePropertyAction
                                PropertyName="IsDropDownOpen"
                                TargetObject="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                Value="True" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <Grid>
                        <Popup
                            x:Name="Popup1"
                            Width="100"
                            MinWidth="80"
                            MaxHeight="300"
                            AllowsTransparency="true"
                            IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}"
                            Placement="Center"
                            PlacementTarget="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}}"
                            PopupAnimation="Fade"
                            StaysOpen="False">
                            <Border
                                Background="{DynamicResource SomeBackgroundBrush}"
                                BorderBrush="{DynamicResource SomeBorderBrush}"
                                BorderThickness="1"
                                TextElement.Foreground="{DynamicResource SomeFontBrush}">
                                <ListBox
                                    x:Name="List1"
                                    ItemTemplate="{TemplateBinding DropDownTemplate}"
                                    ItemsSource="{Binding Path=ListSource, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                    ScrollViewer.CanContentScroll="False"
                                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                    SelectedItem="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}"
                                    Style="{DynamicResource ListBoxNormalStyle}"
                                    VirtualizingPanel.IsVirtualizing="False">
                                </ListBox>
                            </Border>
                            <i:Interaction.Triggers>
                                <ei:DataTrigger Binding="{Binding IsOpen, ElementName=Popup1}" Value="True">
                                    <behaviours:ScrollIntoCenterView TargetObject="{Binding ElementName=List1}" />
                                    <behaviours:Focus TargetObject="{Binding ElementName=List1}" />
                                </ei:DataTrigger>
                            </i:Interaction.Triggers>
                        </Popup>
                        <Border
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            Background="Transparent"
                            BorderThickness="1">
                            <TextBlock
                                x:Name="Presenter"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                FontSize="11"
                                FontWeight="ExtraBold"
                                Text="{Binding SelectedValue}" />
                        </Border>
                    </Grid>
                </Border>
            </ControlTemplate>

注意-
-另请注意,由于某些原因,我不希望将StayOpen设置为True。我想使用StayOpen设置为False

来实现它

0 个答案:

没有答案
相关问题