我有一个场景,组合框可以具有相同的字符串值。对于 exa 组合框可以在下拉列表中有以下值:“Test”、“Test1”、“Test1”、“Test1”、“Test2”、
在选定索引的基础上,我正在填充另一个组合框。我的 Xaml 看起来像:
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding Path=ComboList, Mode=OneWay}"
SelectedIndex="{Binding Path=ComboIndex, Mode=TwoWay}"/ >
</Grid>
ViewModel 看起来像:
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;
}
我面临的问题是 SelectedIndex 不会被解雇,因为我在相同的字符串值之间感到困惑(比如将值从索引 1 处的“Test1”更改为索引 2 处的“Test1”。