当 xaml 中焦点丢失时,wpf 弹出窗口不会关闭

发布于 2024-09-11 15:14:23 字数 2629 浏览 3 评论 0原文

我编写了以下 xaml 代码来显示一个弹出窗口,其中包含扩展器控件中的内容。所有的事情都工作正常,直到单击按钮时弹出窗口打开的位置。但是当我点击远离它时,弹出窗口不会关闭。另外,一旦我单击关闭弹出窗口,我的整个应用程序似乎会冻结一段时间。我试图找出可能是什么问题。谁能帮我找到这个吗?感谢您。

<Style TargetType="{x:Type Expander}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Expander}">
                    <StackPanel>
                        <Button x:Name="btnTask" Content="{TemplateBinding Header}" >
                            <Button.Template>
                                <ControlTemplate TargetType="{x:Type Button}">
                                    <Label Content="{TemplateBinding Content}" Margin="2,0,2,0" />
                                </ControlTemplate>
                            </Button.Template>
                            <Button.ToolTip>
                                <Border CornerRadius="0,2,0,2" BorderBrush="White" BorderThickness="1" Background="SeaShell">
                                    <StackPanel Width="150" Height="Auto">
                                        <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Tag}" Padding="2"/>
                                    </StackPanel>
                                </Border>
                            </Button.ToolTip>
                        </Button>
                        <Popup x:Name="popupTask" Height="200" Width="200" StaysOpen="False">
                            <ContentControl Content="{TemplateBinding Content}" />
                        </Popup>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <EventTrigger SourceName="btnTask" RoutedEvent="ButtonBase.Click">
                            <BeginStoryboard>
                                <Storyboard TargetName="popupTask">
                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" BeginTime="0:0:0" Duration="0:0:3">
                                        <DiscreteBooleanKeyFrame KeyTime="0:0:0.2" Value="True" />
                                    </BooleanAnimationUsingKeyFrames> 
                                </Storyboard> 
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I've written following xaml code to show a popup with the content in a expander control. all the things work ok up to the position where the popup opens when the button is clicked. but the popup wont close when I click away from it. plus as soon as I click away to close the popup my whole application seems to be freezed for a little amount of time. Im trying to figure out what might be the issue. could anyone assist me to find this please ?. Thanks you.

<Style TargetType="{x:Type Expander}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Expander}">
                    <StackPanel>
                        <Button x:Name="btnTask" Content="{TemplateBinding Header}" >
                            <Button.Template>
                                <ControlTemplate TargetType="{x:Type Button}">
                                    <Label Content="{TemplateBinding Content}" Margin="2,0,2,0" />
                                </ControlTemplate>
                            </Button.Template>
                            <Button.ToolTip>
                                <Border CornerRadius="0,2,0,2" BorderBrush="White" BorderThickness="1" Background="SeaShell">
                                    <StackPanel Width="150" Height="Auto">
                                        <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Tag}" Padding="2"/>
                                    </StackPanel>
                                </Border>
                            </Button.ToolTip>
                        </Button>
                        <Popup x:Name="popupTask" Height="200" Width="200" StaysOpen="False">
                            <ContentControl Content="{TemplateBinding Content}" />
                        </Popup>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <EventTrigger SourceName="btnTask" RoutedEvent="ButtonBase.Click">
                            <BeginStoryboard>
                                <Storyboard TargetName="popupTask">
                                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" BeginTime="0:0:0" Duration="0:0:3">
                                        <DiscreteBooleanKeyFrame KeyTime="0:0:0.2" Value="True" />
                                    </BooleanAnimationUsingKeyFrames> 
                                </Storyboard> 
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗喜 2024-09-18 15:14:23

您可以使用 ToggleButton 代替 Button,并根据 ToggleButton 的属性 IsChecked 使用不同的触发器。以下是改变风格的方法:

        <Style TargetType="{x:Type Expander}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <StackPanel>
                            <ToggleButton x:Name="btnTask" Content="{TemplateBinding Header}">
                                <ToggleButton.Template>
                                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                                        <Label Content="{TemplateBinding Content}" Margin="2,0,2,0" />
                                    </ControlTemplate>
                                </ToggleButton.Template>
                                <ToggleButton.ToolTip>
                                    <Border CornerRadius="0,2,0,2" BorderBrush="White" BorderThickness="1" Background="SeaShell">
                                        <StackPanel Width="150" Height="Auto">
                                            <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Tag}" Padding="2"/>
                                        </StackPanel>
                                    </Border>
                                </ToggleButton.ToolTip>
                            </ToggleButton>
                            <Popup x:Name="popupTask" Height="200" Width="200" StaysOpen="False">
                                <ContentControl Content="{TemplateBinding Content}" />
                            </Popup>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger SourceName="btnTask" Property="IsChecked" Value="True">
                                <Setter TargetName="popupTask" Property="IsOpen" Value="True" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

You can use a ToggleButton instead of a Button and use a different trigger, based on the ToggleButton's property IsChecked. Here is how you can change your style:

        <Style TargetType="{x:Type Expander}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <StackPanel>
                            <ToggleButton x:Name="btnTask" Content="{TemplateBinding Header}">
                                <ToggleButton.Template>
                                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                                        <Label Content="{TemplateBinding Content}" Margin="2,0,2,0" />
                                    </ControlTemplate>
                                </ToggleButton.Template>
                                <ToggleButton.ToolTip>
                                    <Border CornerRadius="0,2,0,2" BorderBrush="White" BorderThickness="1" Background="SeaShell">
                                        <StackPanel Width="150" Height="Auto">
                                            <TextBlock TextWrapping="Wrap" Text="{TemplateBinding Tag}" Padding="2"/>
                                        </StackPanel>
                                    </Border>
                                </ToggleButton.ToolTip>
                            </ToggleButton>
                            <Popup x:Name="popupTask" Height="200" Width="200" StaysOpen="False">
                                <ContentControl Content="{TemplateBinding Content}" />
                            </Popup>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger SourceName="btnTask" Property="IsChecked" Value="True">
                                <Setter TargetName="popupTask" Property="IsOpen" Value="True" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文