我正在开发一个遵循 MVVM 的 WPF 应用程序。我在应用程序中有两个组合框。一个绑定到整数列表,另一个绑定到字符串列表。问题是组合框的高度不同(见下图)。知道为什么高度不同吗?两个组合框都没有涉及样式。
看法:
<UserControl.Resources>
<Style TargetType="ComboBox" >
<Setter Property="Margin" Value="5" />
</Style>
<Style TargetType="TextBlock" >
<Setter Property="Margin" Value="5" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</UserControl.Resources>
...
<StackPanel>
<TextBlock Text="{x:Static p:Resources.OutputLayersAsText}" />
<ComboBox ItemsSource="{Binding StringCollection}" />
<TextBlock Text="{x:Static p:Resources.IgzStatAreaSizeText}" />
<ComboBox ItemsSource="{Binding IntegerCollection}" />
</StackPanel>
视图模型:
private ObservableCollection<string> _stringCollection;
public ObservableCollection<string> Stringcollection => _stringCollection ?? (_stringCollection = new ObservableCollection<string>
{
".igz", ".png+.png", ".jpg+.png"
});
private ObservableCollection<int> _integerCollection;
public ObservableCollection<int> IntegerCollection => _integerCollection ?? (_integerCollection = new ObservableCollection<int>
{
8, 12, 16, 24, 32, 48, 64, 96, 128
});
我还尝试了另一个带有枚举集合的组合框,它的高度与整数组合框的高度相似。
显然,默认的 TextBlock 样式也应用于 ComboBox 可视化树中的 TextBlock。不知道为什么它只影响那些显示整数的项目,而不影响那些显示字符串的项目。
由于您已经有了 ComboBox 样式,您可以通过明确定义一个 ItemTemplate 来轻松避免这种行为:
<Style TargetType="ComboBox" >
<Setter Property="Margin" Value="5" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句