MarkupExtension内のDepedencyProperty
-
10-07-2019 - |
質問
MarkupExtension
派生クラス内に DependencyProperty
を持つことは可能ですか?
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>
このような拡張機能を構築することは可能ですか、それとも間違ったツリーを起動するだけですか? (上記のコードはコンパイルして実行しませんが、問題を最もよく説明するためにここに投稿しました。)
解決
いいえ、DependencyObjectから派生したクラスにのみ依存関係プロパティを追加できます。MarkupExtentionはObjectから直接派生します
他のヒント
はい。それは&い問題です。しかし、それは単純な非直感的な答えを持っています。
別のマークアップ拡張機能を作成して、静的リソースを取得します。
そのため、 {StaticResource shapesDS}
DataSetLocator
コードを書くつもりはありませんが、名前またはその他の入力に基づいてProvide値がデータセットを返す必要があります。
次に、xamlを変更して、拡張機能でデータセットロケーター拡張機能の例を使用するようにします Source =&quot; {DataSetLocator name = shapesDS}&quot;
拡張機能がDependencyPropertyを拡張しないのは残念ですが、拡張機能は拡張しません
MarkupExtensionの代わりにIMarkupExtensionを使用するだけで、DependencyObjectを拡張できます。少なくともSilverlight 5では可能ですが、WPFにもあると思います。
所属していません StackOverflow