We often use In-Built markup extensions such as Binding, Static Resource, Template Binding and Relative Source. We will use these markup extensions with curly braces {}. Now in Silverlight 5, we can also write our own Custom Markup Extensions.
Implementing custom markup extension:
Getting ControlName and Control Property, that Markup extensions is binded
IProvideValueTarget ipValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; FrameworkElement targetElement = ipValueTarget.TargetObject as FrameworkElement; PropertyInfo vPropertyInfo = ipValueTarget.TargetProperty as PropertyInfo; // If it’s a dependency property,cast TargetProperty to dependency property instead of propertyinfo
Example:
public class CustomMarkupExtension :MarkupExtension { public string FirstName { get; set; } public string LastName { get; set; } public CustomMarkupExtension() { } public override object ProvideValue(IServiceProvider serviceProvider) { return Convert.ToString(FirstName + " " + LastName); } } xmlns:local="clr-namespace:Sl5Features" <TextBlock Text="{local:CustomMarkupExtension FirstName=Rami,LastName=Reddy}" />