使用maxwidth和右对齐父级将ComboBox拉伸到可用空间

时间:2011-02-14 12:55:59

标签: wpf stretch dockpanel css alignment

我在实现我想要的布局时遇到了问题。 这是我的代码:

<DockPanel DockPanel.Dock="Bottom" HorizontalAlignment="Right" LastChildFill="True">
                <Label DockPanel.Dock="Left" Content="Add new:"/>
                <Button DockPanel.Dock="Right" Content="Add" VerticalAlignment="Center"/>
                <ComboBox VerticalAlignment="Center" MaxWidth="150" HorizontalAlignment="Stretch">
                    <System:String>Item1</System:String>
                    <System:String>Item2</System:String>
                    <System:String>Item3</System:String>
                </ComboBox>
            </DockPanel>

我想要的是按照Label,ComboBox,Button的顺序将三个元素对齐到右边。标签和按钮应占用所需的空间,但我希望ComboBox占用的空间与可能一样多,最多150 px。 当DockPanel未设置为Horizo​​ntalAlignment = Right时,它有用。

任何提示/解决方案?

感谢。

1 个答案:

答案 0 :(得分:3)

使用RightToLeft设置,如下所示:

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid FlowDirection="RightToLeft">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition MaxWidth="150"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
            <Label FlowDirection="LeftToRight" Grid.Column="2" Content="Add new:"/>
            <ComboBox FlowDirection="LeftToRight" Grid.Column="1" VerticalAlignment="Center" MaxWidth="150" HorizontalAlignment="Stretch">
                <ComboBoxItem>Test</ComboBoxItem>
                <ComboBoxItem>Test</ComboBoxItem>
                <ComboBoxItem>Test</ComboBoxItem>
           </ComboBox>
         <Button FlowDirection="LeftToRight"  Grid.Column="0" DockPanel.Dock="Right" Content="Add" VerticalAlignment="Center"/>
     </Grid>
</Grid>

现在正在运行:

Narrow

Wide