0

DockPanel の左または右にドッキングできる StackPanel があります。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>
4

1 に答える 1

0

これを行う 1 つの方法は、値コンバーターを作成することです。プロパティをスタックパネル自体にバインドし、valueconverter 内のドックを取得して、必要なものをすべて返します。このようなもの:

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

コンバータ:

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();
    }
}

水平方向の配置を使用しましたが、必要なものは何でも返すことができます。

于 2012-05-10T15:03:48.810 に答える