我有一个窗口包含一个Viewbox。在那个Viewbox中,我想有一个我已经创建的用户控件的视图。我正在使用MVVM(模型视图视图模型)设计模式。我在网上搜索了大约一个小时,我找不到任何示例或教程来解释如何使用ContentPresenter
来显示UserControl。如何将UserControl加载到(WPF)窗口内的ContentPresenter中?
回答
取决于。
我认为你的主要问题是ContentPresenter
只在控制模板中使用。你不能把它粘在一个窗口中,并期望它显示窗口的内容。我相信你真的需要做的是使用ContentControl托管你的用户界面。
通过将模型绑定到此控件的Content
属性,可以设置包含该模型的预期视图的DataTemplates。我会给你一个简短的例子。请注意,这可能与您的设计不符,但它展示了它们如何结合在一起。首先,对于每个视图,创建一个管理该视图的数据(和交互)的Model(或ViewModel)。
public sealed class Dialog : INotifyPropertyChanged // or DependencyObject
{
public string Input {get;set;} // INPC or DP implementation not shown
public ICommand Cancel {get;set;}
// class definition not shown
}
接下来,定义你的浏览在ContentPresenter显示
<UserControl x:Class="Herp.DialogView"
HideImplementationDetails="true">
<Border BorderBrush="Red" BorderThickness="5">
<TextBlock Text="{Binding Input}" />
<Button Command="{Binding Cancel}">Cancel</Button>
<!-- etc etc -->
</Border>
</UserControl>
接下来,在你的窗口中添加ContentControl中和的DataTemplate
<Window HideImplementationDetailsForBrevityOfXaml="true">
<Window.Resources>
<DataTemplate xmlns:t="clr-namespace:Herp"
DataType="{x:Type t:Dialog}">
<t:DialogView />
</DataTempalte>
</Window.Resources>
<ContentControl Content="{Binding}" />
</Window>
最后设置的DataContext的窗口到您的Dialog
模型。
public MyWindow()
{
InitializeComponent();
DataContext = new Dialog();
}
这是怎样的逻辑流程:创建
- 你的窗口。
- 对话框控件的一个实例在DataContext上设置。
- 在ContentControl中的绑定更新
- 默认的
ContentControl
搜索资源DataTemplateSelector的DataTemplate
其DataType
设置为typeof(Dialog)
- 它发现窗口的资源在这个DataTemplate中
- 的DataTemplate中的内容是loaded并添加为的
ContentControl
视觉孩子在任何时间的0 更改,重复相同的过程。因此,您可以拥有许多不同的模型,每个模型都包含一个不同的包含不同UserControl的DataTemplate,并且每次更新ContentControl上的绑定时都会看到期望的View。使用MVVM,您可以将ViewModel的属性绑定到Content
属性(称为Current或某物),然后根据ViewModel的当前状态将属性中的模型切换为期望值。请注意,在ContentControl中,设置为Content
属性的任何内容都将成为ContentControl的直接子节点的DataContext
。类似于ItemsControl
中的每个ItemsSource
是ItemTemplate
中定义的可视树的DataContext
。
我隐藏了很多关于OK和Cancel的view-viewmodel交互方面的细节,但只要你有一个DataTemplate设置,那么这应该使你的视图在窗口内呈现。
我在我的应用程序的资源字典中定义我的视图模型视图的DataTemplates:
<DataTemplate DataType="{x:Type SaveDocumentChangesVM}">
<SaveDocumentChangesView />
</DataTemplate>
窗口以主视图:
<Window x:Class="CustomDialogWindow"
SizeToContent="WidthAndHeight" ShowInTaskbar="False">
<ContentPresenter Name="DialogContentPresenter" />
</Window>
的构造函数窗口:
public CustomDialogWindow(object viewModel, string dialogTitle)
{
InitializeComponent();
// A datatemplate will allow WPF to figure out how to render the window contents
DialogContentPresenter.Content = viewModel;
if (dialogTitle != null)
{
Title = dialogTitle;
}
}
显示如下对话框:
var vm = new SaveDocumentChangesVM();
var dialog = new CustomDialogWindow(vm, "This is my dialog");
dialog.ShowDialog();
- 1. WPF窗口不ContentPresenter
- 2. 如何将窗口添加到wpf中的usercontrol?
- 3. 如何将窗口边框添加到WPF usercontrol?
- 4. WPF - window.ShowDialog窗口内。加载
- 5. 如何将WPF窗口作为TabControl项目加载到MainWindow中?
- 6. 如何将WPF UserControl从一个窗口移动到另一个窗口?
- 7. 如何将一个WPF usercontrol内WInforms usercontrol?
- 8. 在子窗口中加载UserControl
- 9. 如何加载更快的WPF窗口?
- 10. WPF窗口托管usercontrol
- 11. 如何在BlendDesigner中将UserControl添加到窗口?
- 12. WPF usercontrol命令绑定到窗口viewmodel
- 13. 在UserControl中绑定contentPresenter的内容?
- 14. 如何从UserControl的自定义WPF窗口到父窗口的结果
- 15. 将WPF UserControl添加到TaskPane
- 16. 如何将子窗口仅在WPF的父窗口内移动?
- 17. IronPython WPF加载新窗口
- 18. WPF窗口预加载
- 19. WPF:从XAML加载窗口
- 20. 窗口加载和WPF
- 21. WPF,在UserControl内更新主窗口中的状态栏
- 22. 如何将气球指针添加到WPF中的窗口?
- 23. 如何在UserControl中使用ContentPresenter
- 24. 如何根据UserControl更改WPF中的高度窗口?
- 25. 将usercontrol的datacontext绑定到主窗口
- 26. 如何将加载窗口添加到sencha加载中的图表中
- 27. 如何在代码中将形状添加到WPF窗口?
- 28. 在WPF/C中的UserControl的父窗口中添加子项到StackPanel#
- 29. 将WPF窗口附加到另一个进程的窗口
- 30. 如何在WPF中为功能区窗口设置usercontrol?