1

I have a Silverlight 5.0 Grid with three columns, defined with widths "Auto", "*", and "Auto". Inside the second column, I have another Grid with four columns, defined with width "25*". Each of these four columns is a ComboBox.

Even though the widths are all defined the same, the ComboBoxes "Auto" size themselves, and when I select one, the box itself narrows to just larger than the down arrow, although the items are sized to fit.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
<ContentControl Grid.Column="0" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,1,0,1">
    <ContentPresenter Content="This is a Label" />
</ContentControl>
    <Grid Grid.Column="1" HorizontalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25*"/>
            <ColumnDefinition Width="25*"/>
            <ColumnDefinition Width="25*"/>
            <ColumnDefinition Width="25*"/>
        </Grid.ColumnDefinitions>
        <ComboBox Grid.Column="0" Name="comboView" FontSize="14" Margin="2,1,2,1" >
            <ComboBox.Items>
                <ComboBoxItem Content="Item1" IsSelected="True" />
                <ComboBoxItem Content="Item2" />
            </ComboBox.Items>
        </ComboBox>
        <ComboBox Grid.Column="1" Name="comboField" FontSize="14" Margin="2,1,2,1" >
            <ComboBox.Items>
                <ComboBoxItem Content="Field1" IsSelected="True" />
                <ComboBoxItem Content="Field2" />
            </ComboBox.Items>
        </ComboBox>

...

What have I missed? When I had just one grid, with the first column set to Auto and all the rest set to something like Width=9*, the ComboBoxes did not behave this way.

After further observation, it appears that the HorizontalAlignment="Center" in the inner grid is the culprit. If I remove it, the ComboBoxes return to retaining their size, but they take up the whole outer Column 1, when I want them to take up their necessary space and then be centered within the outer Column.

4

1 回答 1

0

如果我现在理解正确,您最大的问题是您希望所有组合框的宽度相同,都占用相同的空间(即:所有大小都等于具有最大尺寸的组合框)。我相信你已经把其他一切都理顺了。

这在 WPF 中很容易;他们有一个名为“ IsSharedSizeScope ”的属性,它可以均匀地分布网格宽度。

Silverlight 中没有这样的属性。为此,您可能需要绑定它们每个的大小并在后面的代码中完成。这有点hacky,但它应该可以工作。对他们所有人来说都是这样的:

<ComboBox Grid.Column="1" Name="comboField" FontSize="14" Margin="2,1,2,1" Width="{Binding Path=Width}" SizeChanged="comboField_SizeChanged">

然后在后面的代码中:

GridLength Width = new GridLength(10);

private void comboField_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if (e.NewSize.Width > len.Value)
    {
        Width = new GridLength( e.NewSize.Width );
    }
}

编辑:还有这个库似乎可以做你想做的事情。

于 2012-08-29T14:05:49.183 回答