Why does implementing ObservableCollection crash my silverlight application?
Posted
by Sudeep
on Stack Overflow
See other posts from Stack Overflow
or by Sudeep
Published on 2009-06-14T13:32:38Z
Indexed on
2010/04/15
13:03 UTC
Read the original article
Hit count: 272
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
© Stack Overflow or respective owner