I have a scenario where combobox can have same string values. for exa combo box can have following values in drop down:
“Test”,
“Test1”,
“Test1”,
“Test1”,
“Test2”,
On the basis of selected index I am filling another combobox. My Xaml Looks like:
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding Path=ComboList, Mode=OneWay}"
SelectedIndex="{Binding Path=ComboIndex, Mode=TwoWay}"/ >
</Grid>
ViewModel looks like:
class TestViewModel : INotifyPropertyChanged
{
private IList<string> _comboList = new List<string>
{
"Test",
"Test1",
"Test1",
"Test1",
"Test2",
};
public IList<string> ComboList
{
get { return _comboList; }
}
private int _comboIndex;
public int ComboIndex
{
get { return _comboIndex; }
set
{
if (value == _comboIndex)
{
return;
}
_comboIndex = value;
OnPropertyChanged("ComboIndex");
}
}
private void OnPropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Problem I am facing is that SelectedIndex does not get fired incase I am suffling between same string value (like changing value from “Test1” ,present at index 1, to “Test1”, present at index 2.
Instead of binding to a
List<string>, encapsulate the strings, e.g.and bind to a
List<Item>.Then amend your Xaml to specify the DisplayMemberPath
That worked for me.