How to place close [x] in WPF popup

前提是你 提交于 2020-01-24 15:41:10

问题


I have succeed creating a popup using this code in c# and 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]). It will hide when this is clicked, just like dialog in windows forms. Any ideas?Thanks in advance


回答1:


<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




回答2:


In one of our apps we had a similar requirement and we solved it by binding IsOpen to a property of the view model. When you add a button use the Click event handler to set the property to false which will close the popup.



来源:https://stackoverflow.com/questions/8265812/how-to-place-close-x-in-wpf-popup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!