difficulty understanding and using xaml markup extentions

349 Views Asked by At

I am learning on to the concepts of WPF such as data binding, commands, resources, element bindings, styles etc, which use markup extensions extensively, and i am having problem understanding the meaning behind the Markup classes, as they are being used beyond what i understand they should be used for. So here are a few points i need to clear: (all code snippets are from Pro WPF in C# 2010 book)

  1. What is the meaning and use of Static extension? It can be used to declare static resources, which can be declared in as , but this xaml confuses me:

    <Button ... Foreground="{x:Static SystemColors.ActiveCaptionBrush}" >
    

    In {x:Static SystemColors.ActiveCaptionBrush}, what is the role of static here, and what will change if i use x:Dynamic here? The book says this xaml is equivalent to this codebehind:

    cmdAnswer.Foreground = SystemColors.ActiveCaptionBrush;

    This means that if i have a class with static properties, i should be able to use something like this:

    <Button ... Foreground="{x:Static MyClass.SomeStaticProperty}" >
    

    But it didn't work, despite i had created a class, i tried using local:Static (referring to the local namespace) but VisualStudio didn't allow me to use it. What is the proper method of achieving this?

  2. What is the meaning of Binding (beyond obvious meaning, what is happening when i am binding)? It is used for resource binding, or data or element binding. I was able to understand element binding, but binding to objects that are not elements caused problems. For example:

    <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily},
    Path=Source}"></TextBlock>
    

    Here it is binding to the text to the SystemFonts.IconFontFamily property, what is the use of x:static in this case, and how to bind it to a property in a class that i have created? Also how to update the text property of the textfield if the value of the binding target changes? Due to binding, it should update by itself, is this the case?

    All the examples in the book make use of SystemFonts.IconFontFamily, none that i have seen explains the use of such binding, and how to do it for the classes that i create? Some help in this regard is needed. I want to ask more about binding, but i will do so in a separate question about binding only.

  3. Finally, recommend a book or resource that explains what is happening, instead of how to do this and that?

1

There are 1 best solutions below

0
On BEST ANSWER

Answers....

1)

You said ...


... This means that if i have a class with static properties, i should be able to use something like this:

   <Button ... Foreground="{x:Static MyClass.SomeStaticProperty}" >

But it didn't work, despite i had created a class, i tried using local:Static (referring to the local namespace) but VisualStudio didn't allow me to use it. What is the proper method of achieving this?


Well your trial attempt was correct but it was incorrect to what term you have applied that namespace token to.... local namespace token applies to the class that is declared under it so...

   <Button ... Foreground="{x:Static local:MyClass.SomeStaticProperty}" >

Should work just fine provided that SomeStaticProperty is a valid Brush.

In this example, the whole markup was internally equivalent to Binding as ...

   Binding.Source = {x:Type local:MyClass}
   Binding.Path = SomeStaticProperty.

2)

You had an example...


   <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily},     
                             Path=Source}">
   </TextBlock>  

So use the same equivalence from example 1 and apply it to this example...

    <TextBlock Text="{Binding Source={x:Type SystemFonts},     
                              Path=IconFontFamily.Source}">
    </TextBlock>  

3)

I learned this whole thing from MSDN... I dont think we can have any other legitimate source than that.