我有一个 StackPanel,可以停靠在 DockPanel 的左侧或右侧。StackPanel 中的项目应该像祖先一样停靠在同一侧。对于测试,我在 Visual Tree 中获得了祖先的名称,但我不知道如何绑定到 Docking.Dock。提前致谢。
<DockPanel>
<StackPanel x:Name="RightHandContainer" DockPanel.Dock="Right">
<v:MyUsercontrol TextCaption="Hard-Coded Alignment Works" Alignment="Right" />
<v:MyUsercontrol TextCaption="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel, AncestorLevel=1}, Path=Name}"
Alignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel, AncestorLevel=1}, Path=Docking.Dock}" />
<!-- TextCaption is is a dependencyproperty of Type string, works fine ... my Text object automatically gets 'RightHandContainer' -->
<!-- Alignment is is a dependencyproperty of Type Dock, like Docking.Dock ... Binding will not work :( -->
</StackPanel>
</DockPanel>