【问题标题】:WPF MVVM: ComboBox height is different for integersWPF MVVM:整数的组合框高度不同
【发布时间】:2019-05-21 10:57:29
【问题描述】:

我正在开发一个遵循 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
});

我还尝试了另一个带有枚举集合的组合框,它的高度与整数组合框高度相似。

【问题讨论】:

  • 你在这里只展示了故事的一半......那个 UI 的其余部分在哪里?
  • @NawedNabiZada 这些是我在 UI 中仅有的控件。
  • 图片表示别的东西
  • @NawedNabiZada 添加了全图。
  • 您是否重新设计了您的 ComboBox ?如果没有,那么仍然缺少一些东西。

标签: wpf mvvm combobox


【解决方案1】:

显然,默认的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2012-02-29
    • 2018-05-03
    • 2017-02-02
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多