c# - 不影响列宽的 WPF ComboBox

标签 c# wpf wpf-controls

如何告诉 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>

当选择第二个项目时,组合框的大小会发生变化,第三列的大小也会随之变化(从标签的米色背景可以看出)。 enter image description here

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

enter image description here

我想要的是第三列始终具有米色标签的宽度(或该列中的任何其他元素并且不是组合框),并且组合框缩短其文本以适合该宽度。 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/

相关文章:

c# - .NET 4 垃圾收集器的可扩展性

C#从excel文件中读取数据结果###

wpf - 在wpf中设置格式化文本的上标和下标

wpf - 设置 WPF ComboBox 的 SelectedItem

WPF:自定义控件属性已被另一个自定义控件错误注册

wpf - 在运行时和设计时将 ViewModel 分配给 UserControl

c# - 如何正确加载程序集

c# - Windows Metro 应用程序中缺少 XPath

c# - 如何在基于 WPF Prism 的应用程序中使用 .NET 4 SplashScreen?

c# - 将色带控件与 Caliburn Micro 接口(interface)的好方法是什么?