【发布时间】: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,但无济于事。
【问题讨论】:
-
设置
ComboBox的Width属性,或者将ColumnDefinition的Width设置为固定值 -
但这不是我想要的。我把 GridSplitter 放在那里,因为它在我的原始示例中。要求是 ComboBox 与列中的所有其他元素一样宽。
-
还有哪些元素?
-
好吧,在这种情况下只是标签,但显然这只是一个最小的例子,实际上列中还可以有其他元素。
标签: c# wpf wpf-controls