Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox SelectedItem not working as expected

I have a combobox which binds to a collection of Foo on my viewmodel (FooCollection). I also set the SelectedItem property of the combobox to a property on my viewmodel of type Foo called SelectedFoo

I then set FooCollection and SelectedFoo and fire the appropriate OnPropertyChanged events.

My combobox contains the list of Foo, but the item displayed in the combobox is always the first item in the list. However, if you drop down the combobox, the item that is then highlighted is the correct item (SelectedFoo). Therefore, it is selecting the correct item, but not displaying it.

<ComboBox Grid.Row="5"  ItemsSource="{Binding Path=FooCollection}" 
                         SelectedItem="{Binding SelectedFoo, Mode=TwoWay}" 
                         Name="FooSelectionControl"/>

Does anyone know how to fix this?

like image 846
Ben Avatar asked May 26 '11 15:05

Ben


2 Answers

Hmm, it works on my end. What kind of collection are you using?

Screenshot 1

Screenshot 2

    <ComboBox 
        SelectedItem="{Binding SelectedFoo, Mode=TwoWay}"
        ItemsSource="{Binding FooCollection}">
    </ComboBox>

Code behind:

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        FooCollection = new BindingList<Foo>();

        var foo = new Foo("Alpha");
        FooCollection.Add(foo);

        foo = new Foo("Beta");
        SelectedFoo = foo;
        FooCollection.Add(foo);

        foo = new Foo("Gamma");
        FooCollection.Add(foo);
    }

    public Foo SelectedFoo
    {
        get { return (Foo)GetValue(SelectedFooProperty); }
        set { SetValue(SelectedFooProperty, value); }
    }
    public static readonly DependencyProperty SelectedFooProperty =
        DependencyProperty.Register("SelectedFoo", typeof(Foo), typeof(MainWindow), new UIPropertyMetadata(null));

    public BindingList<Foo> FooCollection
    {
        get { return (BindingList<Foo>)GetValue(FooCollectionProperty); }
        set { SetValue(FooCollectionProperty, value); }
    }
    public static readonly DependencyProperty FooCollectionProperty =
        DependencyProperty.Register("FooCollection", typeof(BindingList<Foo>), typeof(MainWindow), new UIPropertyMetadata(new BindingList<Foo>()));

and class Foo,

public class Foo : INotifyPropertyChanged
{
    public Foo(string name)
    {
        _name = name;
    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value) return;

            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public override string ToString()
    {
        return Name;
    }

    #region INotifyPropertyChanged event

    ///<summary>
    ///Occurs when a property value changes.
    ///</summary>
    public event PropertyChangedEventHandler PropertyChanged;


    /// <summary>
    /// Raises the <see cref="PropertyChanged"/> event for
    /// a given property.
    /// </summary>
    /// <param name="propertyName">The name of the changed property.</param>
    protected void OnPropertyChanged(string propertyName)
    {
        //validate the property name in debug builds
        VerifyProperty(propertyName);

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    /// <summary>
    /// Verifies whether the current class provides a property with a given
    /// name. This method is only invoked in debug builds, and results in
    /// a runtime exception if the <see cref="OnPropertyChanged"/> method
    /// is being invoked with an invalid property name. This may happen if
    /// a property's name was changed but not the parameter of the property's
    /// invocation of <see cref="OnPropertyChanged"/>.
    /// </summary>
    /// <param name="propertyName">The name of the changed property.</param>
    [Conditional("DEBUG")]
    private void VerifyProperty(string propertyName)
    {
        Type type = GetType();

        //look for a *public* property with the specified name
        PropertyInfo pi = type.GetProperty(propertyName);
        if (pi == null)
        {
            //there is no matching property - notify the developer
            string msg = "OnPropertyChanged was invoked with invalid property name {0}: ";
            msg += "{0} is not a public property of {1}.";
            msg = String.Format(msg, propertyName, type.FullName);
            Debug.Fail(msg);
        }
    }

    #endregion
}
like image 66
tofutim Avatar answered Nov 04 '22 15:11

tofutim


Maybe try SelectedValue instead of SelectedItem. Also, make sure that Foo.Equals() is implemented correctly.

like image 2
Jörg Reichardt Avatar answered Nov 04 '22 14:11

Jörg Reichardt