简体   繁体   English

项目控制和弹出窗口中的WPF UserControl

[英]WPF UserControl in Items Control and Popup

I am building an application which has an ItemsControl, and my DataTemplate for this contains a ContentPresenter. 我正在构建一个具有ItemsControl的应用程序,为此,我的DataTemplate包含一个ContentPresenter。 I am binding this ContentPresenter to a UserControl property of my ViewModel. 我将此ContentPresenter绑定到ViewModel的UserControl属性。 I would like to be able to allow the user to click a button on the UserControl which then shows that UserControl in a new popup window, and then when closed, it reverts back to being in the ItemsControl. 我希望能够允许用户单击UserControl上的按钮,然后在新的弹出窗口中显示该UserControl,然后在关闭时将其恢复为显示在ItemsControl中。

I can get this to show the UserControl in the popup window no problems, but have no idea how to revert it back to being in the ItemsControl. 我可以使它在弹出窗口中显示UserControl没问题,但不知道如何将其还原回ItemsControl。

Any suggestions for this approach would be greatly appreciated. 对于这种方法的任何建议将不胜感激。

thanks 谢谢

Put simply you should not keep a UserControl (a UI element) inside your "ViewModel". 简而言之,您不应该在“ ViewModel”中保留UserControl(UI元素)。 Your ViewModels should hold the State of your UI and not the UI itself. 您的ViewModels应该保存UI的状态 ,而不是UI本身。 Define two different UserControls for the two parts you want it in but this time Bind them to the same ViewModel properties. 为所需的两个部分定义两个不同的UserControl,但这一次将它们绑定到相同的ViewModel属性。

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

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