简体   繁体   English

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

[英]How to place close [x] in WPF popup

I have succeed creating a popup using this code in c# and wpf 我已经成功使用c#和wpf中的此代码创建了一个弹出窗口

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

I use the code below to hide it when one mouse-click outside it and it works right. 当在其外部单击鼠标时,我使用下面的代码将其隐藏,并且可以正常使用。

myPopup.IsOpen = true;
myPopup.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;
myPopup.StaysOpen = false;
myPopup.Height = 500;
myPopup.Width = 500;
myPopup.IsOpen = true;

My problem is that I would like to add a close button(or something like [x]). 我的问题是我想添加一个关闭按钮(或类似[x]的东西)。 It will hide when this is clicked, just like dialog in windows forms. 单击此按钮时,它将隐藏,就像Windows窗体中的对话框一样。 Any ideas?Thanks in advance 有什么想法吗?谢谢

In one of our apps we had a similar requirement and we solved it by binding IsOpen to a property of the view model. 在我们的一个应用程序中,我们有一个类似的要求,我们通过将IsOpen绑定到视图模型的属性来解决此问题。 When you add a button use the Click event handler to set the property to false which will close the popup. 添加按钮时,请使用Click事件处理程序将属性设置为false,这将关闭弹出窗口。

<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>

In the mouse_DownHandled event handler, you can add the code to close th popup 在mouse_DownHandled事件处理程序中,您可以添加代码以关闭弹出窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM