友
我正在使用MVVM绑定ListBox SelectedItem
,但似乎它不起作用。该列表已成功绑定,但未选择任何项目。
XAML Page
<ListBox x:Name="lsbReadingChapter" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
ItemsSource="{Binding QuranText}" Style="{StaticResource ListBoxStyle1}"
Grid.Row="1">
....
</ListBox>
MVVM代码
public class QuranTextViewModel : INotifyPropertyChanged
{
DataSource ds = null;
ObservableCollection<ArabicTextWithTranslation> _quranText;
public QuranTextViewModel(Chapter c)
{
ds = new DataSource();
QuranText = new ObservableCollection<ArabicTextWithTranslation>();
List<ArabicTextWithTranslation> texts = ds.getArabicTextWithTranslation(c);
Recent r = DataSource.Recents;
SelectedItem = (from grp in texts
where grp.ArabicText.ChapterID == r.ChapterID && grp.ArabicText.AyaID == r.AyaID
select grp).FirstOrDefault();
foreach (ArabicTextWithTranslation t in texts)
{
QuranText.Add(t);
}
}
public ObservableCollection<ArabicTextWithTranslation> QuranText
{
get { return _quranText; }
set
{
if (_quranText != value)
{
_quranText = value;
OnPropertyChanged("QuranText");
}
}
}
ArabicTextWithTranslation _selectedItem;
public ArabicTextWithTranslation SelectedItem
{
get {
return _selectedItem;
}
set
{
if (_selectedItem != value)
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
}
}
SelectedItem
无效,但在调试模式下,我可以看到SelectedItem
中的值。
如何设置SelectedItem
和Highlight
SelectedItem?