我有一個自定義控件Cake,它包含兩個名爲Slice和Filling的DependencyProperties。 如何創建一種讓我可以使用切片的風格,同時也讓我設計切片?我的蛋糕也吃了它:綁定容器和它的內容
<Style TargetType={x:Type local:Cake}>
//I don't like setting DataContext Here
<Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType={x:Type local:Cake}>
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
//This is how I display a slice
<ContentPresenter Content={Binding Slice}/>
//This is how cake decorations are displayed
<ItemsPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Slice">
<Setter.Value>
//Design Slice Here - it's easy to override when I want
<Slice Filling={Binding Filling}> // it's just in a setter.
</Setter.Value>
</Setter>
<Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}/>
</Style>
選項我已經試過:
我不能使用一個用戶控件,因爲我想允許指定的內容,這顯然不符合用戶控件工作。見Here.
我不喜歡上面的例子,因爲我必須設置的DataContext蛋糕容器的自我,這就意味着用戶不能使用的DataContext爲他們 綁定。
我無法使用RelativeSource綁定Filling屬性,因爲有幾個 的蛋糕,Style將不知道哪一個是正確的父級。見 Here.
我可以用一個片元素, 直接更換內容呈現,但因爲它是一個模板,我鬆訪問切片任何地方 模板之外。雖然我可能會將視覺樹切成片,但這感覺是一場維護噩夢。
我基本上希望每個蛋糕有一個切片,並用
<Cake.Slice>
<DockPanel>
<Rectangle Background= 「Blue」/>
<Rectangle Background= 「Blue」/>
<Rectangle Background=「{Binding Filling}」/>
</DockPanel>
</Cake.Slice>
同時也給它一個默認外觀要能設置。
編輯: 顯然,我的風格不工作,前提是我引用Cake.dll,而不是蛋糕項目。爲什麼會這樣?
你有沒有想要在蛋糕中實現的圖片? –
@AyyappanSubramanian是的,只是加了一個。 – bwall