C# 如何在wp8中突出显示ListBox SelectedItem?
朋友们 我正在使用MVVM绑定C# 如何在wp8中突出显示ListBox SelectedItem?,c#,windows-phone-8,C#,Windows Phone 8,朋友们 我正在使用MVVM绑定列表框SelectedItem,但它似乎不起作用。列表已成功绑定,但未选择任何项 XAML页面 <ListBox x:Name="lsbReadingChapter" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" ItemsSource="{Binding QuranText}" Style="{StaticResource ListBoxStyle1}" Grid.Row="1"> .... &
列表框SelectedItem
,但它似乎不起作用。列表已成功绑定,但未选择任何项
XAML页面
<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");
}
}
}
}
公共类QuranTextViewModel:INotifyPropertyChanged
{
数据源ds=null;
可观察收集_quranText;
公共QuranTextViewModel(c章)
{
ds=新数据源();
QuranText=新的ObservableCollection();
列表文本=ds.getArabicTextWithTranslation(c);
最近r=数据源。最近;
选择编辑项=(从文本中的grp)
其中grp.ArabicText.ChapterID==r.ChapterID&&grp.ArabicText.AyaID==r.AyaID
选择grp.FirstOrDefault();
foreach(阿拉伯文,文本翻译为t)
{
QuranText.Add(t);
}
}
公共可观测收集QuranText
{
获取{return\u quranText;}
设置
{
如果(_quranText!=值)
{
_quranText=数值;
OnPropertyChanged(“QuranText”);
}
}
}
带翻译的阿拉伯文本_selectedItem;
公共阿拉伯文本,带翻译选择编辑项
{
得到{
返回_selectedItem;
}
设置
{
如果(_selectedItem!=值)
{
_选择editem=值;
OnPropertyChanged(“SelectedItem”);
}
}
}
}
SelectedItem
不工作,但在调试模式下,我可以在SelectedItem
中看到值
如何设置
SelectedItem
和Highlight
SelectedItem?您可以应用我在这里写的解决方案:如果您不想使用VisualManager路线,那么您也可以将颜色绑定到您的模型,以下是我的解决方案: