我有一个自定义控件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