Search code examples
wpfbindingstackpaneldockpanelrelativesource

Binding to ancestor StackPanel DockPanel.Dock


I have a StackPanel that can be docked to the Left or Right inside of a DockPanel. The items in the StackPanel should dock to the same side like the ancestor does. For Testing i get the Name of the ancestor in Visual Tree but i have no idea how to bind to Docking.Dock. Thanks in advance.

<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>

Solution

  • One way to do it is to create a value converter. Bind the property to the stackpanel itself, and grab the dock inside the valueconverter and return whatever you need to. Something like this:

    <Window.Resources>
        <app:TestConverter x:Key="TestConverter" />
    </Window.Resources>
    <DockPanel>
        <StackPanel x:Name="RightHandContainer" DockPanel.Dock="Right">
            <TextBlock Text="Test" HorizontalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel, AncestorLevel=1}, Converter={StaticResource TestConverter}}"  />
        </StackPanel>
    </DockPanel>
    

    converter:

    public class TestConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            HorizontalAlignment alignment = HorizontalAlignment.Left;
    
            StackPanel stackPanel = value as StackPanel;
            if (stackPanel != null)
            {
                Dock dock = DockPanel.GetDock(stackPanel);
                switch (dock)
                {
                    case Dock.Left: alignment = HorizontalAlignment.Left; break;
                    case Dock.Right: alignment = HorizontalAlignment.Right; break;
                }
            }   
            return alignment;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    I used horizontal alignment, but you can return whatever you need to.