0
我正在開發從ItemsControl
派生的自定義控件。在generic.xaml文件我創建了該控件的樣式,也定義了ItemTemplate
:如何從樣式中定義的DataTemplate綁定到TemplatedParent?
<Style TargetType="local:MyItemsControl">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="Red">
<!-- Other things in here -->
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
我想將Border
在DataTemplate
的Background
屬性綁定到MyItemsControl
的依賴項屬性。
如果發現這裏有幾個問題,建議在綁定中使用MyItemsControl
的元素名稱,但只有在定義使用該控件的ItemTemplate
時才起作用。我還嘗試綁定到RelativeSource
,將local:MyItemsControl
定義爲祖先類型。
沒有工作。我在這裏錯過了什麼?
在ControlTemplate中可以使用TemplateBinding。但是這隻能在ControlTemplate中使用......我會嘗試祖先的方式。 – MVCible
我嘗試過 - 但無法讓它正常工作。所以要麼這不起作用 - 要麼我犯了一個錯誤... – Spontifixus