#78 – Setting the Value of a Content Property By Setting Child Element
September 28, 2010 3 Comments
You can set the value of a property using an XAML attribute. Consider setting the Text property of a TextBox using an attribute:
<TextBox Height="23" Width="120" Text="Eat Muesli!"/>
You can also set the same property value using property element syntax:
<TextBox Height="23" Width="120"> <TextBox.Text> Eat Muesli! </TextBox.Text> </TextBox>
There is an even simpler way to specify the value of the Text property. Because the TextBox class has identified its Text property as a content property, the value of the Text property can be specified directly as the single child of the TextBox XAML element:
<TextBox Height="23" Width="120"> Eat Muesli! </TextBox>
Great concept for your site. I have it saved as a favorite. I am new at this. Question – How do I determine with properties of a specific class is designated as the content property?
Thanks Gary. For content properties, you could work through MSDN (takes a while), or use reflection to figure out which properties are content properties. (ContentPropertyAttribute). Or take a look at the post listed below–I posted some code showing how to find content properties and I also list all controls in PresentationFramework.dll that have content properties, along with the name of the content property.
http://stuff.seans.com/2010/09/28/building-a-list-of-types-that-have-content-properties/
Pingback: #971 – Items Property is a Content Property | 2,000 Things You Should Know About WPF