如何在WPF弹出窗口中放置关闭[x]

3

我已成功使用此c#和wpf代码创建了一个弹出窗口

<Popup Name="myPopup" IsOpen="True">
<Label Name="myLabel" Content="This is a popup!" Background="AliceBlue" Foreground="Blue"/>
        </Popup> 

我使用以下代码将其隐藏,当鼠标在其外单击时它可以正常工作。
myPopup.IsOpen = true;
myPopup.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;
myPopup.StaysOpen = false;
myPopup.Height = 500;
myPopup.Width = 500;
myPopup.IsOpen = true;

我的问题是我想添加一个关闭按钮(或类似于[x]的东西)。当该按钮被点击时,它会隐藏,就像Windows窗体中的对话框一样。有什么建议吗?提前感谢。

2个回答

3

在我们的一个应用中,我们有类似的要求,并通过将IsOpen绑定到视图模型的属性来解决它。当添加按钮时,请使用Click事件处理程序将属性设置为false,这将关闭弹出窗口。


如果我想要将其最小化到状态栏,并在需要时最大化,就像Windows窗体一样。这是否可能?有什么想法吗? - Cheruiyot Felix
1
@KiuFelix 那么 Popup 可能不是正确的控件类型。最好发布一个单独的问题,详细说明您要实现的目标,看看人们想出哪些想法。 - ChrisWue

2
<Popup Name="myPopup" IsOpen="True">
    <StackPanel>
        <Label Background="AliceBlue" Foreground="Blue" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" MouseDown="mouse_DownHandled">
        x
        </Label>
        <Label Name="myLabel" Content="This is a popup!" Background="AliceBlue" Foreground="Blue"/>
    </StackPanel>
</Popup>

在mouse_DownHandled事件处理程序中,您可以添加代码来关闭弹出窗口。

太棒了gprsant!如果我想像Windows表单一样将其最小化到状态栏并在需要时最大化,这是否可能?有什么想法吗? - Cheruiyot Felix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接