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

时间:2016-06-09 09:47:10

标签: wpf binding wpf-controls

我正在构建一个继承ItemsControl的CustomControl,并为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>

这是转换器类:

   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根据对接方向(就像我在转换器中编码的那样)。

1 个答案:

答案 0 :(得分:1)

您无需任何转换器即可获得所需的结果。只需为DockPanel.DockTop值的Bottom附加属性添加带有两个DataTriggers的StackPanel样式。

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>