github编辑

ContentControl

The ContentControl displays data according to a data template.

Common Properties

Property
Description

Content

The content to display in the control

Reference

ContentControlarrow-up-right

Source code

ContentControl.csarrow-up-right

Display content

At its simplest, a ContentControl displays the data assigned to its Contentarrow-up-right property.

For example:

<ContentControl Content="Hello World!"/>

Will display the string "Hello World!". The Content property is the control's default property and so the above example can also be written as:

<ContentControl>Hello World!</ContentControl>

If you assign a control to a ContentControl then it will display the control, for example:

<ContentControl>
  <Button>Click Me!</Button>
</ContentControl>

Display content with templates

So far so uninteresting. Where ContentControl becomes useful is in tandem with data bindingarrow-up-right and data templatesarrow-up-right. By setting the ContentTemplatearrow-up-right property one can specify how the data in the Content property is displayed. For example given the following view models:

Note: The following examples assume an instance of MainWindowVieModel is assigned to the Window's DataContext. See the section on DataContextarrow-up-right for more information.

We can display the student's first and last name in a ContentControl using the ContentTemplate property:

Student first and last name

For more information see the data templatesarrow-up-right section.

最后更新于