WPF - 在 ItemsPanelTemplate 的 StackPanel 中将方向 属性 绑定到 DockPanel.Dock

WPF - Bind Orientation property to DockPanel.Dock in ItemsPanelTemplate's StackPanel

我正在构建一个 CustomControl,它继承了 ItemsControl 并且有一个用于 ItemsPanelTemplate 的 StackPanel。 这是样式:

   <Style TargetType="{x:Type MyCustomControl}">
      <Setter Property="ItemsPanel">
         <Setter.Value>
            <ItemsPanelTemplate>
               <StackPanel Orientation="{Binding Path=DockPanel.Dock, Converter={StaticResource PositionToOrientationConverter}}"/>
            </ItemsPanelTemplate>
         </Setter.Value>
      </Setter>
      <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
               <Grid >
                  <Border Background="Black" Height="0"/>
                  <ItemsPresenter/>
               </Grid>
            </ControlTemplate>
         </Setter.Value>
      </Setter>
   </Style>

这是转换器 Class:

   Public Class PositionToOrientation
  Implements IValueConverter

  Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
     Dim position As Dock = value
     Select Case position
        Case Dock.Top
           Return Orientation.Horizontal
        Case Dock.Bottom
           Return Orientation.Horizontal
        Case Dock.Left
           Return Orientation.Vertical
        Case Dock.Right
           Return Orientation.Vertical
     End Select
     Return Dock.Top
  End Function

  Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
     Throw New NotImplementedException
  End Function
   End Class

但是好像忽略了方向属性。

编辑:我想要获得的功能是,当我将 MyCustomControl 放入 DockPanel 时,StackPanel 根据对接定向(就像我在 Converter 中编码的那样)。

您不需要任何转换器即可获得所需的结果。只需为 DockPanel.Dock 添加一个带有两个 DataTriggers 的 StackPanel 样式,为 TopBottom 值附加 属性。

DataTriggers 中的绑定使用 ItemsControl 作为它们的 RelativeSource 并将 属性 路径放在括号中,因为它是附加的 属性.

<ItemsControl DockPanel.Dock="Top">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel>
                <StackPanel.Style>
                    <Style TargetType="StackPanel">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=(DockPanel.Dock), RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                         Value="Top">
                                <Setter Property="Orientation" Value="Horizontal"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=(DockPanel.Dock), RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                         Value="Bottom">
                                <Setter Property="Orientation" Value="Horizontal"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </StackPanel.Style>
            </StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>