0
我使用MVVM模式結合組合框的SelectedIndex值在視圖模型的int:爲什麼在存在重複條目時,WPF組合框上的SelectedIndex更新不會更新?
<ComboBox ItemsSource="{Binding DropdownListChoices}" Margin="5,2,5,1" Width="320" Height="23"
Style="{StaticResource comboBoxWithErrorHandling}" SelectedIndex="{Binding SelectedComboBoxIndex}">
查看模型:
public class FieldViewModel : ObservableObject, IDataErrorInfo
{
private int _selectedComboBoxIndex;
public int SelectedComboBoxIndex
{
get { return _selectedComboBoxIndex; }
set
{
if (_selectedComboBoxIndex != value)
{
_selectedComboBoxIndex = value;
RaisePropertyChanged("SelectedComboBoxIndex");
}
}
}
// ...
}
在代碼的不同部分,我填充DropdownListChoices 。假設元素是例如A,B,C,A,D。正確選擇B,C或D會導致SelectedComboBoxIndex獲得期望值(分別爲1,2或4)。但選擇A會將SelectedComboBoxIndex設置爲0,而不管第一個還是第二個A被選中。在選擇第二個A時,我期望所選索引爲3.
爲什麼會發生這種情況?有沒有不同的方式來實現我想要做的事情,即獲得絕對列表索引選擇?
它也會混淆用戶。不知道用戶如何區分A和A. – Josh
謝謝。這是一個公平的觀點,喬希,但考慮到這個代碼只是複製了SharePoint功能的一個子集,所以混亂是由設計決定的,而不是爲了避免:P –