WPF - всплывающее окно не закрывается при нажатии снаружи - PullRequest
0 голосов
/ 17 апреля 2019

У меня есть пользовательский элемент управления, который использует шаблон элемента управления, как показано в блоке кода ниже.Мне нужно показать всплывающее окно, когда пользователь нажимает на элемент управления.Как только он / она щелкает вне всплывающего окна, оно должно закрываться.
При MouseLeftButtonUp всплывающее окно отображается, однако остается открытым при нажатии вне всплывающего окна.
Обратите внимание, что я установил фокус на элемент всплывающего списка.как только открывается pop, используя 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>

Note -
- Также обратите внимание, я не хочу устанавливать StayOpen в True по какой-то причине вне области действияпроблемы.Я хочу добиться этого, используя StayOpen, установленный в False

...