Markup extensions are placeholders in XAML that are used to resolve a property at runtime. A markup extension enables you to extend XAML and set any property that can be set in XAML using attribute syntax. xNull, x:Array, :StaticResource, and DynamicResource are some common markup extensions. The System.Windows.Markup namespace defines many of the markup extension classes. These class names end with the suffix Extension; however, you can omit the Extension suffix. For example, in XAML, you represent the NullExtension markup class as x:Null. A custom markup extension is a class created by extending the MarkupExtension class or the IMarkupExtension interface. It is useful in scenarios where you need to provide functionality or behavior that is beyond the scope of existing built-in markup extensions. Consider that you want to bind a ListBox that will bind to XML data as shown below but for some reason you don't want to use an ObjectDataProvider.<ListBox ItemsSource="<some way to bind the data> Source=Books.xml, Path=/Book/Title}" If you want to declaratively bind as shown above, you will need to use a custom markup extension.The steps to create and use such an extension are: Create a WPF application named WPFXMLBinding
Add an XML file named Books to the application that has the following contents: <?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book Title="Coma" Author="Robin Cook" />
<Book Title="The Color Purple" Author="Alice Walker" />
<Book Title="The White Tiger" Author="Aravind Adiga" />
<Book Title="A Thousand Splendid Suns" Author="Khaled Hoseini" />
</Books>
Add reference to System.Xml.Linq assembly.
Using the Project->Add Class menu option, add a class named CustomXMLExtension to the application.
Add the following code to the class: namespace WPFXMLBinding
{
public class CustomXMLExtension : MarkupExtension
{
public string Source { get; set; }
public string Path { get; set; }
private static List<string> Parse(string file, string path)
{
XDocument xdoc = XDocument.Load(file); string[] text = path.Substring(1).Split('/');
string desc = text[0].ToString();
Read more: C# Corner
QR:
Add an XML file named Books to the application that has the following contents: <?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book Title="Coma" Author="Robin Cook" />
<Book Title="The Color Purple" Author="Alice Walker" />
<Book Title="The White Tiger" Author="Aravind Adiga" />
<Book Title="A Thousand Splendid Suns" Author="Khaled Hoseini" />
</Books>
Add reference to System.Xml.Linq assembly.
Using the Project->Add Class menu option, add a class named CustomXMLExtension to the application.
Add the following code to the class: namespace WPFXMLBinding
{
public class CustomXMLExtension : MarkupExtension
{
public string Source { get; set; }
public string Path { get; set; }
private static List<string> Parse(string file, string path)
{
XDocument xdoc = XDocument.Load(file); string[] text = path.Substring(1).Split('/');
string desc = text[0].ToString();
Read more: C# Corner
QR:
0 comments:
Post a Comment