Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不带Listbox/Itemssource的数据模板_C#_Wpf_Binding_Datatemplate - Fatal编程技术网

C# 不带Listbox/Itemssource的数据模板

C# 不带Listbox/Itemssource的数据模板,c#,wpf,binding,datatemplate,C#,Wpf,Binding,Datatemplate,我是WPF的新手,我很难理解DataTemplate、DataContext和当然还有MVVM的精妙之处。 我的问题是: 我想为Person类创建一个DataTemplate,它包含一个包含三个标签的网格,分别是名字、姓氏和年龄。 创建DataTemplate后,我希望在mainwindow中向主网格添加一个人,而不使用列表框或包含ItemSource的组件 总结一下我的问题-我想知道是否有一种方法可以使用DataTemplate可视化对象,在这种方法中,我不必将对象添加到集合,然后添加到Ite

我是WPF的新手,我很难理解DataTemplate、DataContext和当然还有MVVM的精妙之处。 我的问题是: 我想为Person类创建一个DataTemplate,它包含一个包含三个标签的网格,分别是名字、姓氏和年龄。 创建DataTemplate后,我希望在mainwindow中向主网格添加一个人,而不使用列表框或包含ItemSource的组件

总结一下我的问题-我想知道是否有一种方法可以使用DataTemplate可视化对象,在这种方法中,我不必将对象添加到集合,然后添加到ItemsSource


谢谢

您需要的是ContentPresenter,此元素使用ContentTemplate属性指定的DataTemplate显示其Content属性指定的数据。因此,在您的示例中,您可以使用以下内容:

<ContentPresenter ContentTemplate={StaticResource YourTemplate}
                  Content={Binding PathToYourPerson}/>

作为参考,这是控件(如按钮)在其模板中用于呈现其内容的元素