Wednesday, June 29, 2011

Markup Extensions in XAML


You can specify a value to an attribute using curly braces ‘{}’ is called as Markup Extensions in XAML. The most commonly used markup extensions in WPF are Binding, StaticResource, DynamicResources, RelativeSource, TemplateBinding etc. These all markup extensions are default provided by WPF. If you want you can create your own Markup Extension you can achieve it by deriving MarkupExtension class. Nested Markup Extensions also possible in XAML.

<Window.Resources>
<Style x:Key="MyStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Red" />
</Style>
</Window.Resources>

<StackPanel>
      <Button Content="Click Me!" Style="{StaticResource MyStyle}" />
</StackPanel>

In above example Style attribute of Button uses StaticResource Markup Extension.

2 comments:

  1. Is the custom markup extension and what we talking above are same. ??

    ReplyDelete
    Replies
    1. In this post no custom markup extensions used. You can create your own markup extension as per your requirement,

      Delete