编辑:
我再次查看了这个问题,发现比以前提出的解决方案要简单得多。这个想法是重写 ComboBox 类的 MeasureOverride 函数并提供有限的可用空间,其中宽度等于 ComboBox 的 MinWidth 属性。为此,我们创建 ComboBox 派生类:
public class MyComboBox
: ComboBox
{
protected override Size MeasureOverride(Size constraint)
{
return base.MeasureOverride(new Size(MinWidth, constraint.Height));
}
}
然后我们在控件中使用它来代替将 HorizontalContentAlignment 设置为 Stretch 的 ComboBox:
<local:MyComboBox Grid.Column="1" Margin="5" MinWidth="110"
HorizontalContentAlignment="Stretch">
<ComboBoxItem Content="Very loooooooooooooooooooooooooooooooooooooooong text"/>
<ComboBoxItem Content="Normal text"/>
</local:MyComboBox>
之前提出的,太复杂的解决方案:
您可以将第二个 ComboBox 的 MaxWidth 属性绑定到剩余的空间(您可以计算它):
<Window x:Class="Example.MainWindow"
xmlns:local="clr-namespace:Example"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="150" Width="300">
<Window.Resources>
<local:SubstractConverter x:Key="SubstractConverter"/>
</Window.Resources>
<GroupBox Header="Group Header" Margin="5">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
x:Name="ScrollViewer1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox MinWidth="100" Grid.Column="0" Margin="5" MaxWidth="150"
x:Name="ComboBox1"/>
<ComboBox Grid.Column="1" Margin="5" MinWidth="110">
<ComboBox.MaxWidth>
<MultiBinding Converter="{StaticResource SubstractConverter}">
<Binding ElementName="ScrollViewer1" Path="ActualWidth"/>
<Binding ElementName="ComboBox1" Path="ActualWidth"/>
<Binding ElementName="ComboBox1" Path="Margin.Left"/>
<Binding ElementName="ComboBox1" Path="Margin.Right"/>
<Binding Path="Margin.Left" RelativeSource="{RelativeSource Self}"/>
<Binding Path="Margin.Right" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</ComboBox.MaxWidth>
<ComboBoxItem Content="Very loooooooooooooooooooooooooooooooooooooooong text"/>
<ComboBoxItem Content="Normal text"/>
</ComboBox>
</Grid>
</ScrollViewer>
</GroupBox>
</Window>
我从滚动查看器的 ActualWidth 中减去第一个组合框的 ActualWidth、它的边距和第二个组合框的边距。为了减少要减去的元素,您可以在 ComboBox1 周围放置一个边框。然后,您将减去边框的 ActualWidth 而不是 ComboBox1 属性。
此处使用的转换器:
public class SubstractConverter
: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values != null && values.Any())
{
var result = System.Convert.ToDouble(values.First());
var toSubstract = values.Skip(1);
foreach (var number in toSubstract)
{
result -= System.Convert.ToDouble(number);
}
return result;
}
return 0d;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}