MarkupExtension中的DepedencyProperty

时间:2009-11-30 05:50:50

标签: wpf binding c#-3.0 dependency-properties markup-extensions

是否可以在DependencyProperty派生类中拥有MarkupExtension

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
        }
    }
}

扩展程序用于以下代码段。

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

甚至可以构建这样的扩展,还是我只是咆哮错误的树? (上面的代码不会编译和运行,但我在这里发布它以最好地说明问题)。

3 个答案:

答案 0 :(得分:5)

不,您只能将依赖项属性添加到从DependencyObject派生的类中,MarkupExtention直接从Object派生

答案 1 :(得分:0)

答案 2 :(得分:-1)

只需使用IMarkupExtension而不是MarkupExtension,就可以扩展DependencyObject。至少在Silverlight 5中你可以,但我认为WPF也有它。