【问题标题】:WPF ComboBox that does not influence column width不影响列宽的 WPF ComboBox
【发布时间】:2017-05-05 18:27:08
【问题描述】:

如何告诉 ComboBox 不影响其所在网格的列宽?

这是一个最小的例子:

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label>This is some text</Label>
        <Label Grid.Row="1">This is some text</Label>
        <GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Stretch"/>
        <Label Grid.Column="2" Background="Beige" HorizontalAlignment="Right">This is some text</Label>
        <ComboBox Grid.Row="1" Grid.Column="2">
            <ComboBoxItem IsSelected="True">This is some text</ComboBoxItem>
            <ComboBoxItem>This is some really lengthy text that is really long</ComboBoxItem>
        </ComboBox>
    </Grid>
</StackPanel>

The ComboBox changes its size when the second item is selected, and together with it, the size of the third column is changed (as can be seen by the beige background of the label).

这也有米色标签中的文本有时会超出可见区域的效果,即使有足够的空间来完全显示它:

我想要的是第三列始终具有米色标签的宽度(或列中的任何其他元素,而不是 ComboBox),并且 ComboBox 缩短其文本以适应该宽度。 ComboBox的弹出部分可以更大,我这里只说按钮部分。我尝试设置一个 ComboBox,将 TextBlock 内容和 TextTrimming 设置为 CharacterEllipsis,但无济于事。

【问题讨论】:

  • 设置ComboBoxWidth属性,或者将ColumnDefinitionWidth设置为固定值
  • 但这不是我想要的。我把 GridSplitter 放在那里,因为它在我的原始示例中。要求是 ComboBox 与列中的所有其他元素一样宽。
  • 还有哪些元素?
  • 好吧,在这种情况下只是标签,但显然这只是一个最小的例子,实际上列中还可以有其他元素。

标签: c# wpf wpf-controls


【解决方案1】:

以下是供您参考的代码

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

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

【讨论】:

    【解决方案2】:

    这里应该为你做:

    <StackPanel Orientation="Vertical">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Label>This is some text</Label>
            <Label Grid.Row="1">This is some text</Label>
            <GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Stretch"/>
            <Label x:Name="Label" Grid.Column="2" Background="Beige" HorizontalAlignment="Right">This is some text</Label>
            <ComboBox Grid.Row="1" Grid.Column="2" Width="{Binding ElementName=Label, Path=ActualWidth, Mode=OneWay}">
                <ComboBoxItem IsSelected="True">This is some text</ComboBoxItem>
                <ComboBoxItem>This is some really lengthy text that is really long</ComboBoxItem>
            </ComboBox>
        </Grid>
    </StackPanel>
    

    【讨论】:

    • 这在示例中有效,谢谢。在我的实际应用程序中,整个列是在运行时创建的,这并不容易,但我想我必须编写代码隐藏,在该列中选择一个随机的非 ComboBox 控件并绑定到该列。
    • 只是为了给个结论,我只是在我的真实代码中实现了我上面提到的代码隐藏,它按要求工作,所以再次感谢你。
    • @Flash 没问题,很高兴它有帮助。
    【解决方案3】:

    你可以像使用 Combobox 的 Width 属性

    <ComboBox Grid.Row="1" Grid.Column="2" Width="200">
    

    文本换行也可能对您有所帮助,以下链接有一个示例,说明如何为组合框使用文本换行:ComboBox TextWrap Binding

    【讨论】:

    • 再说一次,这不是我想要的。要求是 ComboBox 与列一样宽,但不影响列的大小。不允许使用固定宽度。
    【解决方案4】:

    设置组合框控件的 MaxWidth。它将允许组合框扩展到该值。如果你想要一个固定的宽度,那么你需要设置组合框的宽度属性。

    【讨论】:

    • 这不是我想要的。 ComboBox 应该与列中的所有其他元素一样宽,而不是某个固定值。列中的哪些元素(不是在我的最小示例中)是在运行时决定的。
    • 然后你用*设置列的宽度并设置组合框Horizo​​ntalAlignment="Stretch"。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多