无DataTemplate的列表框/的ItemsSource [英] Datatemplate without Listbox/Itemssource

查看:110
本文介绍了无DataTemplate的列表框/的ItemsSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的WPF,和我有认识的DataTemplate,DataContext中的微妙困难和ofcourse - MVVM。
我的问题是这样的:
我想创建的类Person一个DataTemplate - 包含其中包含三个标贴网格 - 为姓,名字和年龄。
创建我的DataTemplate后,我想一个人添加到在主窗口的主电网,没有一个列表框或具有的ItemSource一个组成部分。

I'm fairly new to WPF, and I'm having a difficulty understanding the subtlety of DataTemplate, DataContext and ofcourse - MVVM. My problem is this: I want to create a DataTemplate for the class Person - which contains a grid which contains three lables - for the First Name, Last Name and Age. After creating my DataTemplate, I wish to add a person to the main grid in the mainwindow, without a listbox or a component which has ItemSource.

要总结一下我的问题 - 我想知道是否有与在的DataTemplate其中我没有把它们添加到集合,然后到的ItemsSource可视化对象的方式。

To sum up my question - I want to know if there's a way to visualize objects with DataTemplate in which i dont have to add them to a collection and then to ItemsSource.

谢谢!

推荐答案

您需要的是一个Content presenter,此元素显示使用由它的ContentTemplate属性指定的DataTemplate中其内容属性指定的数据。所以,在你的榜样,你可以使用以下内容:

What you need is a ContentPresenter, this element displays the data specified by its Content property using the DataTemplate specified by its ContentTemplate property. So, in your example, you can use the following:

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

作为参考,这是作为按钮使用他们的模板中呈现的内容控制等元素。

For reference, this is the element that controls such as Buttons use within their template to render their content.

这篇关于无DataTemplate的列表框/的ItemsSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆