在Wpf(mvvm)中更改WrapPanel的方向

时间:2014-08-12 06:54:51

标签: c# wpf

我的WPF程序设计出了问题。我的XAML看起来像这样:

<StackPanel Orientation="{Binding Orientation}">
   <Border ... /> 
   <Border ... /> 
   <Border ... />
   <Border Margin="2"
           VerticalAlignment="Stretch"
           CornerRadius="3"
           Padding="4">
      <WrapPanel>
         <TextBlock VerticalAlignment="Center"
                    Text="Time Range:"
                    Width="66" />
         <controls:WinFormsWrapper EndDateTime="{Binding EndDateTime,Mode=TwoWay}"
                                   InitialEndDateTime="{Binding InitialEndDateTime}"
                                   InitialStartDateTime="{Binding InitialStartDateTime}"
                                   StartDateTime="{Binding StartDateTime,Mode=TwoWay}"/>
      </WrapPanel>
   </Border>
   <Border ... /> 
</StackPanel>

在WrapPanel内部,您可以看到我的WinFormsWrapper。在我的Viewmodel中,我得到了一个属性Orientation,它将决定如何定位StackPanel。如果Orientation设置为vertical,我希望WrapPanel现在水平填充整个控件,反之亦然。

有没有人知道这样做的好方法?

修改

该属性的类型为System.Windows.Controls.Orientation,可能的值为:System.Windows.Controls.Orientation.VerticalSystem.Windows.Controls.Orientation.Horizontal

1 个答案:

答案 0 :(得分:1)

如果您将Background设置为WrapPanel,则面板为完整尺寸 我认为您应该使用DockPanel而不是WrapPanel使用属性值LastChildFill="True"

您的错误来自无效值。为Orientation属性设置有效的起始值

private Orientation orientation = Orientation.Horizontal;