MarkupExtension
派生類中可能有DependencyProperty
嗎?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>
是否有可能建立這樣的擴展,或者我只是吠叫錯誤的樹? (上面的代碼不會編譯和運行,但我在這裏發佈它以最好地說明問題)。
在那種情況下綁定標記擴展是什麼?它的屬性可以自行綁定,因此它們是依賴屬性。 – Narek 2012-08-21 06:58:57
@Narek - 根據MSDN綁定標記擴展沒有依賴屬性,這是有道理的,因爲你不能不調用DependencyObject.SetValue和DependencyObject.GetValue實現依賴屬性,你不能調用那些沒有從DependencyObject繼承。另外,我現在不能測試它,但我不認爲你可以使用綁定綁定自己的屬性,那是你不能做{Binding Converter = {綁定...}} – Nir 2012-08-21 07:21:55
其實我沒有在WPF中的任何經驗,但在Silverlight 5中,綁定本身的屬性絕對有可能使用綁定。下面是一個例子:「{綁定用戶名,RelativeSource = {RelativeSource FindAncestor,AncestorType = UserControl}}」。 – Narek 2012-08-22 08:21:29