views:

545

answers:

2

Hi,

I have a combobox whose ItemsSource property is bound to an ObservableCollection property and its SelectedIndex property is bound to an integer property respectively.

<ComboBox Name="cmbDealt" ItemsSource="{Binding Path=DealList, Mode=TwoWay}" SelectedIndex="{Binding Mode=TwoWay, Path=DealIndex}"></ComboBox>
<CheckBox IsChecked="{Binding Mode=TwoWay, Path=SomeCondition}" Content="Some Condition"></CheckBox>

My data structure looks like

 private ObservableCollection<string> m_DealList = null;
    private int m_DealIndex = 0;
    private bool m_SomeCondition = false;

    public ObservableCollection<string> DealList
    {
        get
        {
            if (m_DealList == null)
                m_DealList = new ObservableCollection<string>();
            else
                m_DealList.Clear();

            if (m_SomeCondition)
            {
                m_DealList.Add("ABC");
                m_DealList.Add("DEF");
            }
            else
            {
                m_DealList.Add("UVW");
                m_DealList.Add("XYZ");
            }
            return m_DealList;
        }
    }

    public int DealIndex
    {
        get { return m_DealIndex; }
        set
        {
            if (value != -1)
            {
                m_DealIndex = value;
            }
        }
    }

    public bool SomeCondition
    {
        get { return m_SomeCondition; }
        set
        {
            m_SomeCondition = value;
            OnPropertyChanged("DealList");
            OnPropertyChanged("DealIndex");
        }
    }

Now the application loads successfully. However, when the user changes the SelectedIndex of the ComboBox to 1 from 0 and then checks the checkbox (so as to call the "DealIndex" property changed event), the application crashes.

I am not sure why this could be happening. Can someone shed some light and propose a solution?

TIA... Sudeep

A: 

One option would be to change the binding from selectedindex to selecteditem. you can accomplish the same thing. I origially started by trying to change selectedindex and I was never successful. I think it maybe read only.

zachary
Hi Zachary, SelectedIndex works well in normal scenarios. Only when we try to refresh-n-rebind the collection and then set the SelectedIndex immediately, that it fails. I was reading on some blog that it seems that OnPropertyChanged() of the ObservableCollection takes slight time, hence when the OnPropertyChanged() of SelectedIndex gets fired just after the OnPropertyChanged() of ObservableCollection, there possibly is no data to set the index of. I tested this out in a crude way and it possibly turns out to be true. But I am still baffled by this and am waiting for a proper approach.
Sudeep
A: 

You don't need to fire

OnPropertyChanged("DealList");

as the property is an ObservableCollection. That means it implements the observer pattern and when items are added and removed it will fire on its own.

You don't need to have the binding mode of the ObservableCollection set to TwoWay, unless the user can update the items through the user interface. It doesn't appear that your code allows for that.

You also could just use the SelectedIndexChanged on the ComboBox instead of performing an action on the CheckBox, unless there are multiple CheckBoxes. That's just a thought about providing a better user experience.

greglev