简体   繁体   中英

WPF ComboBox truncating items

I found a strange behavior when using ComboBox with enum items. I noticed that the popup that displays the entries when I click on the ComboBox truncates long items. I figured out that this happens because I define a TextBlock style with a fixed Width. What is strange is that, the Width only affects the ComboBox when I use enum items. It does not happen If I use string ones instead.

Here's a picture with what's going on. The third item should be "VeryLongTypeName".

在此处输入图片说明

Here is the code sample written according with the MVVM pattern.

The UserControl XAML:

<UserControl.DataContext>
    <local:SampleViewModel/>
</UserControl.DataContext>
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Width" Value="70"/>
            <Setter Property="Margin" Value="0,0,5,0"/>
        </Style>
    </StackPanel.Resources>
    <DockPanel>
        <TextBlock Text="Items"/>
        <ComboBox ItemsSource="{Binding ItemsList}" SelectedItem="{Binding Item}"/>
    </DockPanel>
    <DockPanel>
        <TextBlock Text="String Items"/>
        <ComboBox ItemsSource="{Binding StringItemsList}" SelectedItem="{Binding StringItem}"/>
    </DockPanel>
</StackPanel>

The SampleViewModel code:

public class SampleViewModel
{
    public enum SomeType { Type1, Type2, VeryLongTypeName };

    public IEnumerable<SomeType> ItemsList
    {
        get { return (SomeType[])Enum.GetValues(typeof(SomeType)); }
    }

    public SomeType Item { get { return ItemsList.First(); } set { } }

    public IEnumerable<string> StringItemsList
    {
        get { return ItemsList.Select(type => type.ToString()); }
    }

    public string StringItem { get { return StringItemsList.First(); } set { } }
}

If you build the code sample, in the second ComboBox below the one from the picture, things go smoothly with string values.

I have the following questions:

  1. Why does changing the type affect the graphics?

  2. How do I fix the ComboBox display when using enum?

Any help is welcomed.

Your textblock style is for all textblocks. The content of the combobox is also displayed with textblocks and you limited the width of textblocks to 70. Use a key for your style or set another textblock style for the comboboxes.

The problem also happens when listing items with a ListBox. I used Live Property Explorer to see what's going on. Both cases render the content in a TextBlock, but only when using enum values the style defined as resource is applied. Don't know why this happens, but that's how it is.

To fix the problem for enum and possibly other types except string, I added the following style, based on @Mardukar's idea:

<Style TargetType="ComboBoxItem">
    <Style.Resources>
        <Style TargetType="TextBlock" BasedOn="{x:Null}"/>
    </Style.Resources>
</Style>

@Fredrik's idea of changing the ComboBox.ItemTemplate also works.

For ListBox, the style needs to have TargetType either ListBoxItem or ListBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM