Reputation: 2315
I tried reading the MSDN article on markup extensions, but I can’t find out what they are (the article discusses what they do).
I cannot find a clear explanation of why we need markup extensions. If we can access a control object directly, why would we need a markup extension to access a binding object?
Do we need markup extensions so XAML is aware of the code behind (otherwise there is no way to get access any of the built in classes)? But then how can we access all the control types?
Upvotes: 7
Views: 5894
Reputation: 12998
From https://wpftutorial.net/XAML.html:
Markup extensions are dynamic placeholders for attribute values in XAML. They resolve the value of a property at runtime.
Markup extensions are surrouded by curly braces (Example:
Background="{StaticResource NormalBackgroundBrush}"
).WPF has some built-in markup extensions, but you can write your own, by deriving from MarkupExtension. These are the built-in markup extensions:
Binding
To bind the values of two properties together.
StaticResource
One time lookup of a resource entry
DynamicResource
Auto updating lookup of a resource entry
TemplateBinding
To bind a property of a control template to a dependency property of the control
x:Static
Resolve the value of a static property.
x:Null
Return null
The first identifier within a pair of curly braces is the name of the extension. All preciding identifiers are named parameters in the form of Property=Value. The following example shows a label whose Content is bound to the Text of the textbox. When you type a text into the text box, the text property changes and the binding markup extension automatically updates the content of the label.
<TextBox x:Name="textBox"/> <Label Content="{Binding Text, ElementName=textBox}"/>
Regarding what a markup extension is composed of:
All markup extensions derive from the abstract MarkupExtension class and override its ProvideValue method. The naming convention is to append the word Extension to the subclass’s name (only the Binding class does not follow the pattern).
The XAML parser allows markup extensions to be created within {curly braces} and it also allows you to omit the Extension suffix when using a markup extension, if you want to.
Example code:
<!--- Configure a binding markup extension via the special curly brace syntax --->
<TextBox Text="{Binding Path=Name}" Width="120"/>
<!--- Configure a binding markup extension via the standard element syntax --->
<Checkbox Content="Is person alive?">
<Checkbox.IsChecked>
<Binding Path="IsAlive"/>
</Checkbox.IsChecked>
</Checkbox>
In the XAML above, take a look at the TextBox’s Text property, and the CheckBox’s IsChecked property. They both use the Binding markup extension to bind their values to a property on the data context (a Person object).
Upvotes: 3
Reputation: 69
Rationale for markup extensions:
XAML is simple, which is a good thing. It is just an XML-based language used to declare objects and the relationships between them. One side effect of being simple is that it can be verbose. This cumbersome verbosity was one of the main reasons why the concept of markup extensions was introduced. A markup extension can be used to turn many lines of XAML into one concise expression...
Another side effect of XAML’s simplicity is that it does not have any “built in” knowledge of common artifacts used by WPF or the CLR; such as resource references, data binding, a null value, arrays, static members of a class, etc. Since XAML can be an integral part of application development there needs to be some way for developers to express those ideas in it.
<TextBox >
<TextBox.Text>A text in TextBox</TextBox.Text>
</TextBox>
<TextBox Text="{x:Static system:Environment.UserName}" />
This latter syntax also provides a way to use values other than a literal string (i.e., which is a new object) such as an already constructed object or a static object in our assembly. In this sense markup extensions are objects which decide how a property's going to be set at runtime.
Upvotes: 2
Reputation: 184356
Markup extensions are not about access but extending functionality of markup (as the name implies) by doing whatever you want them to, like creating associations (Binding
, x:Reference
) or getting the type of a class (x:Type
).
They can be used for just about anything, they are only necessary where the markup does not suffice on its own.
Upvotes: 7