我正在用Silverlight构建一个自定义的ItemsControl,它允许项目在运行时水平或垂直显示。如何将ItemsPanel的Orientation属性绑定到父控件的Orientation属性?我试过使用TemplateBinding (它在ControlTemplate中工作),但似乎不能在ItemsPanelTemplate中工作,我是不是做错了什么?
<Style TargetType="CustomItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="{TemplateBinding Orientation}" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>发布于 2009-03-29 12:05:41
使用RelativeSource:
<Style TargetType="CustomItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel Orientation="{Binding Orientation, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CustomItemsControl}}}" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>评论后编辑: Silverlight不支持RelativeSource,但Colin Eberhardt的this post解释了如何手动实现它。
https://stackoverflow.com/questions/694368
复制相似问题