ComboBox values disappears after selected when objects used for display

Posted by Gakk on Stack Overflow See other posts from Stack Overflow or by Gakk
Published on 2010-05-14T16:45:31Z Indexed on 2010/05/18 9:30 UTC
Read the original article Hit count: 178

Filed under:
|
|

I have a combobox where I want to display objects and have enum values returned. When first opened the combobox displays the items as supposed, but after a value is chosen it seems to disappear from the list. But if the combobox is active I can use the keyboard to navigate up and down between the other values, so they are in the list but only invisible.

I have created a little test application to show my problem. When started the application shows the combobox with all the choices (the two first are type of Object, the third is a String):

All choices shown on startup

After the blue line is selected and when the combobox is opened again this line is missing:

Blue line selected and is missing in popup

When the line with the text "Green" is selected that line is still showing:

Green line selected and still shown in popup

If I had chosen the red line the only thing that would still be in the list is the test "Green".

I am using .NET Framework 3.5.

Any hints or tips to why the elements disappears?


Here are all the code needed after starting a blank project in Visual Studio.

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Test
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private ColorComboBoxValue _activeColor;
        public ColorComboBoxValue ActiveColor
        {
            get { return _activeColor; }
            set
            {
                _activeColor = value;
                Debug.WriteLine("ActiveColor: " + _activeColor.Color);
            }
        }
    }

    public class ColorList : List<ColorComboBoxValue> { }

    public class ColorComboBoxValue
    {
        public Color Color { get; set; }
        public Object Object { get; set; }
    }

    public enum Color
    {
        Red,
        Blue,
        Green
    }
}

MainWindow.xaml:

<Window x:Class="Test.MainWindow" x:Name="window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:Test"
        Title="ComboBoxTest" Height="100" Width="200">

    <Window.Resources>
        <local:ColorList x:Key="ColorList">
            <local:ColorComboBoxValue Color="Red">
                <local:ColorComboBoxValue.Object>
                    <Path Data="M0,0 L0,30 60,30 60,0 Z" Fill="Red"/>
                </local:ColorComboBoxValue.Object>
            </local:ColorComboBoxValue>
            <local:ColorComboBoxValue Color="Blue">
                <local:ColorComboBoxValue.Object>
                    <Path Data="M0,0 L0,30 60,30 60,0 Z" Fill="Blue"/>
                </local:ColorComboBoxValue.Object>
            </local:ColorComboBoxValue>
            <local:ColorComboBoxValue Color="Green">
                <local:ColorComboBoxValue.Object>
                    <System:String>Green</System:String>
                </local:ColorComboBoxValue.Object>
            </local:ColorComboBoxValue>
        </local:ColorList>
    </Window.Resources>

    <ComboBox ItemsSource="{Binding Source={StaticResource ColorList}}"
              SelectedItem="{Binding ActiveColor, ElementName=window}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding Path=Object}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Window>

© Stack Overflow or respective owner

Related posts about xaml

Related posts about combobox