DepedencyProperty within a MarkupExtension

3.4k Views Asked by At

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).

3

There are 3 best solutions below

5
On BEST ANSWER

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

1
On

Just use IMarkupExtension instead of MarkupExtension and you can extend DependencyObject. At least in Silverlight 5 you can, but I would assume WPF also has it.

0
On

Yea.. it’s an ugly problem.. However it has a simple non intuitive answer. Create another markup extension to get the static resource. So instead of using {StaticResource shapesDS}

You would create a new MarkupExtension called DataSetLocator

I'm not going to write the code but the Provide value would need to return your dataset based on a name or some other input.

Then you change your xaml to have your extension use the dataset locator extension example Source="{DataSetLocator name=shapesDS }"

It’s too bad that extensions don’t extend DependencyProperty but they don’t.