gpt4 book ai didi

wpf - 我该如何让那个该死的 wpf 弹出窗口消失?

转载 作者:行者123 更新时间:2023-12-02 01:22:33 25 4
gpt4 key购买 nike

当我使用弹出窗口时,它似乎一直在徘徊。在下面的代码中,我通过覆盖控件模板将弹出窗口附加到文本框,并使弹出窗口在文本框具有焦点时出现。当您按 Tab 键切换到屏幕上的下一个元素时,弹出窗口会消失,但如果您只是按 Alt 键切换到其他应用程序,则弹出窗口会保留在前台。我该如何摆脱它?

<Window x:Class="DropDownPicker.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StackPanel>
<TextBox Text="hello">
<TextBox.Style>
<!-- Simple TextBox -->
<Style
TargetType="{x:Type TextBox}">
<Setter
Property="KeyboardNavigation.TabNavigation"
Value="None" />
<Setter
Property="FocusVisualStyle"
Value="{x:Null}" />
<Setter
Property="AllowDrop"
Value="true" />
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type TextBox}">
<Grid>
<Border
x:Name="Border"
Background="{DynamicResource WindowBackgroundBrush}"
BorderBrush="{DynamicResource SolidBorderBrush}"
BorderThickness="1"
Padding="2"
CornerRadius="2">

<Grid>
<!-- The implementation places the Content into the ScrollViewer. It must be named PART_ContentHost for the control to function -->
<ScrollViewer
Margin="0"
x:Name="PART_ContentHost"
Style="{DynamicResource SimpleScrollViewer}"
Background="{TemplateBinding Background}" />

<Popup
x:Name="thePopup"
IsOpen="False">
<Border
BorderBrush="Red"
BorderThickness="5">
<TextBlock
Text="Hellssss" />
</Border>
</Popup>
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger
Property="IsFocused"
Value="True">
<Setter
TargetName="thePopup"
Property="IsOpen"
Value="True" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
</TextBox>
<TextBox
Text="ssss" />
</StackPanel>
</Grid>
</Window>

最佳答案

您是否尝试过将 StaysOpen 属性设置为 False

如果StaysOpenTrue(默认值),它将保持打开状态,直到控件不再处于焦点状态。如果为False,它将保持打开状态,直到在Popup 控件外部发生鼠标或键盘事件,这可能是在使用 alt-tab 键时出现的情况。您可能需要稍微调整它才能使其表现得像您想要的那样,但这可能是一个起点。

关于wpf - 我该如何让那个该死的 wpf 弹出窗口消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1871602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com