2009-11-30 11 views
10

DependencyPropertyMarkupExtension派生クラス内に含めることはできますか?MarkupExtension内のDepedencyProperty

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> 

このような拡張機能を構築することは可能ですか、間違ったツリーを吠えるだけですか? (上のコードはコンパイルして実行しませんが、問題を最もよく説明するためにここに掲載しています)。

答えて

5

いいえ、あなただけのDependencyObjectから派生するクラスに依存関係プロパティを追加することができ、MarkupExtentionがオブジェクト

+0

その場合、バインディングマークアップ拡張はどうですか?プロパティは独自にバインドできるため、依存プロパティです。 – Narek

+0

@Narek - MSDNによれば、バインディングマークアップ拡張に依存プロパティはありません。DependencyObject.SetValueとDependencyObject.GetValueを呼び出さずに依存プロパティを実装することはできず、DependencyObjectから継承せずに呼び出すことはできません。また、私は今、それをテストすることはできませんが、私はバインディング自体のプロパティにバインディングを使用することはできませんと思う、それはできません{バインディングコンバータ= {バインディング...}} – Nir

+0

実際に私は持っていないWPFの経験はありますが、Silverlight 5ではバインディング自体のプロパティにバインディングを使用することは間違いありません。次に例を示します。 "{UserName、RelativeSource = {RelativeSource FindAncestor、AncestorType = UserControl}}をバインドします。 – Narek

0

いやから直接派生します。..それは、単純な非直感的な答えを持っている。しかしながら醜い問題です。 静的リソースを取得する別のマークアップ拡張を作成します。 ので、代わりの名称または他の入力に基づいてデータセットを返すために必要があるだろう{StaticResource shapesDS}

あなたは、私がコードを書くつもりはないDataSetLocator

と呼ばれる新しいMarkupExtensionを作成しますが提供する値を使用して。

次に、あなたの拡張機能は、それは拡張子がしたDependencyPropertyを拡張しませんが、そうではないことを残念だデータセットロケータ拡張例にSource="{DataSetLocator name=shapesDS }"

を使用持っているあなたのXAMLを変更します。

-1

MarkupExtensionの代わりにIMarkupExtensionを使用するだけで、DependencyObjectを拡張できます。少なくともSilverlight 5では可能ですが、私はWPFもそれを持っていると仮定します。

+6

WPFにはIMarkupExtensionがありません。 – Brian

関連する問題