Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DepedencyProperty within a MarkupExtension

Is it possible to have a DependencyProperty within a MarkupExtension derived class?

public class GeometryQueryExtension : MarkupExtension
{
    public XmlDataProvider Source { get; set; }

    public string XPath { get; set; }

    public static readonly DependencyProperty ArgumentProperty = DependencyProperty.RegisterAttached(
        "Argument",
        typeof(string),
        typeof(GeometryQueryExtension)); // this wont work because GeometryQueryExtension is not a DependencyProperty

    public string Argument
    {
        get
        {
            return (string)GetValue(ArgumentProperty); // this wont even compile because GeometryQueryExtension doesnt derive from a class which has GetValue
        }
        set
        {
            SetValue(ArgumentProperty,value);// this wont even compile because GeometryQueryExtension doesnt derive from a class which has SetValue
        }
    }
}

The extension is used in the following snippet.

<Label.Content>
    <local:GeometryQueryExtension Source="{StaticResource shapesDS}">
        <local:GeometryQueryExtension.XPath>
            /Shapes/Geometry/{0}
        </local:GeometryQueryExtension.XPath>
        <local:GeometryQueryExtension.Argument>
            <Binding XPath="Geometry"/> <!-- will throw exception when processing this bind -->
        </local:GeometryQueryExtension.Argument>
    </local:GeometryQueryExtension>
</Label.Content>

Is it even possible to build such an extension or am i just barking up the wrong tree ? (the code above wont compile and run, but i posted it here to best illustrate the problem).

like image 491
Andrew Keith Avatar asked Nov 30 '09 05:11

Andrew Keith


People also ask

What is a dependency property?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.

How does dependency property work internally?

A DependencyProperty maintains a static reference of all the DependencyProperty you register in WPF object hierarchy. It maintains a HashTable named PropertyFromName which it uses internally to get the DependencyProperty object. So in other word, each dependencyProperty object is registered in a global HashTable.

How do you declare a dependency property?

Follow the steps given below to define custom dependency property in C#. Declare and register your dependency property with system call register. Provide the setter and getter for the property. Define an instance handler which will handle any changes that occur to that particular instance.

What is the biggest feature of dependency property?

Arguably the biggest feature of a dependency property is its built-in ability to provide change notification. The motivation for adding such intelligence to properties is to enable rich functionality directly from declarative markup.


1 Answers

No, you can only add dependency properties to classes that are derived from DependencyObject, MarkupExtention is derived directly from Object

like image 153
Nir Avatar answered Sep 21 '22 17:09

Nir