拖动GridSplitter上的WPF组合框大小

时间:2011-05-26 20:41:32

标签: wpf combobox wpf-controls

如何锚定一个combox框,以便在我拖动分割器时它会自动调整大小。

我尝试使用stackpanel无效,甚至设置Horizo​​ntalAlignment和Horizo​​ntalContentAlignment来拉伸;这是行不通的。

我需要组合框与拖动部分一样宽。在非wpf中你只是锚定了组合,在wpf中该选项不可用。

<StackPanel Orientation="Horizontal">
    <Label Content="View By" 
            Padding="5" 
            Height="30" />
    <ComboBox Name="ddSelector" 
            Height="25" 
            Width="auto" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" 
            HorizontalContentAlignment="Stretch" 
            Margin="16,0,0,0" />
</StackPanel>

堆栈面板位于第0行,网格分割器位于第1行。

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="275"></ColumnDefinition>
    <ColumnDefinition Width="auto"></ColumnDefinition>
    <ColumnDefinition Width="350"></ColumnDefinition>
    <ColumnDefinition Width="auto"></ColumnDefinition>
    <ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>

<GridSplitter Width="5" 
    Margin="0,36,0,5" 
    BorderThickness="4" 
    BorderBrush="Transparent" 
    Grid.Column="1" 
    HorizontalContentAlignment="Center" 
    HorizontalAlignment="Center" 
    ResizeBehavior="PreviousAndNext"></GridSplitter>

2 个答案:

答案 0 :(得分:4)

使用Grid代替StackPanel。这样做。

<Grid>
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition Width="*"/>
   </Grid.ColumnDefinitions>

   <Label ../>
   <ComboBox Grid.Column="1" HorizontalAlignment="Stretch" .../>

</Grid>

答案 1 :(得分:2)

这样的事情:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0">Some stuff here</Label>
        <GridSplitter 
            ResizeDirection="Rows"
            Grid.Row="1"
            Height="3"
            HorizontalAlignment="Stretch" />
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Content="View By" 
            Padding="5" 
            Height="30" />
            <ComboBox Name="ddSelector" Grid.Column="1"
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" 
            HorizontalContentAlignment="Stretch" 
            Margin="16,0,0,0" />
        </Grid>
    </Grid>
</Window>