如何告诉 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>
当选择第二个项目时,组合框的大小会发生变化,第三列的大小也会随之变化(从标签的米色背景可以看出)。
这也会导致米色标签中的文本有时超出可见区域,即使有足够的空间来完全显示它:
我想要的是第三列始终具有米色标签的宽度(或该列中的任何其他元素并且不是组合框),并且组合框缩短其文本以适合该宽度。 ComboBox的弹出部分可以更大,我这里只说按钮部分。我尝试设置一个 ComboBox,将 TextBlock Content 和 TextTrimming 设置为 CharacterEllipsis,但无济于事。
最佳答案
这里应该适合你:
<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>
关于c# - 不影响列宽的 WPF ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41244004/