Jason Rainwater demonstrates content control basics in WPF 4.5. This lesson is an extract from WPF 4.5 Programming LiveLessons by Sams: 4.5 Hours of Video Instruction In WPF 4.5 Programming LiveLessons, Jason Rainwater shows how to create rich, next-generation applications with WPF using Visual Studio 2013. Master panels, content and item controls, code behind, binding, resources, styles, templates, behaviors, animations, custom controls, localization, navigation, MVVM, and more!
I'm working on a project using MVVM with WPF and I'm in a difficult spot. When I create a Button and a ContentControl in a window where the button changes the content of the ContentControl, it works fine. Dec 1, 2008 - Note that the Content property is bound using the TemplateBinding to. The content of this button (whether its simple text, or a complex set of. By changing this to a ContentControl, you can bind the Foreground of the content.
New features on 4.5 are covered, including binding to static properties, automatically updating the source of a data binding via delay, and enabling smooth scrolling with ScrollUnit on VirtualizingStackPanel. About the Author Jason Rainwater is a senior consultant with Veracity Solutions.
His specialties include an in-depth knowledge of WPF, Silverlight, and XAML; he has designed and built enterprise scalable WPF and Silverlight smart client applications utilizing MVVM, Prism, and many more patterns and frameworks. Skill Level Beginner to intermediate Learn How To Create a WPF application starting from the very first project creation Understand the basics of controls Use style controls Use Binding Write custom controls Use advanced XAML techniques such as Behaviors Navigate advanced concepts such as localization, navigation, and MVVM Who Should Take This Course Developers looking for a practical introduction to WPF for creating desktop business applications Course Requirements Familiar with C# programming Experience using Visual Studio Click here for a complete Table of Contents.
WPF Content Model. 4 minutes to read. Contributors. In this article Windows Presentation Foundation (WPF) is a presentation platform that provides many controls and control-like types whose primary purpose is to display different types of content. To determine which control to use or which control to derive from, you should understand the kinds of objects a particular control can best display. This topic summarizes the content model for WPF control and control-like types. The content model describes what content can be used in a control.
This topic also lists the content properties for each content model. A content property is a property that is used to store the content of the object. Classes That Contain Arbitrary Content Some controls can contain an object of any type, such as a string, a object, or a that is a container for additional items. For example, a can contain an image and some text; or a can contain the value of. WPF has four classes that can contain arbitrary content. The following table lists the classes, which inherit from.
Class that contains arbitrary content Content A single arbitrary object. A header and a single item, both of which are arbitrary objects. A collection of arbitrary objects. A header and a collection of items, all of which are arbitrary objects. Controls that inherit from these classes can contain the same type of content and treat the content in the same way.
The following illustration shows one control from each content model that contains an image and some text. Controls That Contain a Single Arbitrary Object The class contains a single piece of arbitrary content. Its content property is.
The following controls inherit from and use its content model:. The following illustration shows four buttons whose is set to a string, a object, a, and a that contains an and a. Four buttons that have different types of content For an example of how to set the property, see. Controls That Contain a Header and a Single Arbitrary Object The class inherits from and displays content with a header. It inherits the content property, from and defines the property that is of type; therefore, both can be an arbitrary object.
The following controls inherit from and use its content model:. The following illustration shows two objects. The first has objects as the and the. The is set to a that contains an and a.
The is set to a that contains a and a. The second has a string in the and a in the.
TabControl that uses different types in the Header property For an example of how to create objects, see. Controls That Contain a Collection of Arbitrary Objects The class inherits from and can contain multiple items, such as strings, objects, or other elements.
Its content properties are. Is typically used to populate the with a data collection. If you do not want to use a collection to populate the, you can add items by using the property. The following controls inherit from and use its content model:. The following illustration shows a that contains these types of items:.
A string. A that contains an and a. ListBox that contains multiple types of objects Controls That Contain a Header and a Collection of Arbitrary Objects The class inherits from and can contain multiple items, such as strings, objects, or other elements, and a header. It inherits the content properties, and, and it defines the property that can be an arbitrary object.
The following controls inherit from and use its content model:. Classes That Contain a Collection of UIElement Objects The class positions and arranges child objects. Its content property is. The following classes inherit from the class and use its content model:. For more information, see. Classes That Affect the Appearance of a UIElement The class applies visual effects onto or around a single child. Its content property is.
The following classes inherit from and use its content model:. The following illustration shows a that has (is decorated with) a around it. TextBlock that has a Border Classes That Provide Visual Feedback About a UIElement The class provides visual cues to a user. For example, use an to add functional handles to elements or provide state information about a control. The class provides a framework so that you can create your own adorners.
WPF does not provide any implemented adorners. For more information, see. Classes That Enable Users to Enter Text WPF provides three primary controls that enable users to enter text. Each control displays the text differently.
The following table lists these three text-related controls, their capabilities when they display text, and their properties that contain the control's text. Control Text is displayed as Content property Plain text Formatted text Hidden text (characters are masked) Classes That Display Your Text Several classes can be used to display plain or formatted text. You can use to display small amounts of text. If you want to display large amounts of text, use the, or controls. The has two content properties:. When you want to display text that uses consistent formatting, the property is often your best choice. If you plan to use different formatting throughout the text, use the property.
The property is a collection of objects, which specify how to format text. The following table lists the content property for, and classes. Control Content property Content property type Document Document Document The implements the interface; therefore, all three classes can take a as content. Classes That Format Your Text and its related classes allow you to format text. Objects contain and format text in and objects. The two primary types of objects are elements and elements. A element represents a block of text, such as a paragraph or list.
An element represents a portion of text in a block. Many classes specify formatting for the text to which they are applied. Each has its own content model. For more information, see the. See also. Feedback.