5
我有以下數據的模板(和相應的視圖模型,未示出):無法使用明確列表框選擇的SelectedItem =空 - MVVM
<DataTemplate DataType="{x:Type logic:SnapshotListViewModel}">
<ListBox ItemsSource="{Binding Snapshots}" />
</DataTemplate>
ItemsSource綁定到快照的列表,發現內部視圖模型。我的目標是清除SelectedItem,因此列表框會回到其初始未選中狀態。視圖模型實現了IPropertyNotified。
我加在XAML像這樣綁定:
<ListBox SelectedItem={Binding SelectedSnapshot} .... />
在視圖模型,我設置SelectedSnapshot = null,但什麼也沒發生,即使RaisePropertyChanged被稱爲對物業。
我再次嘗試SelectedIndex而不是SelectedItem。仍然沒有運氣。
我終於找到了解決方案,我將在下面詳細介紹。