使用LINQ获取1-2-3分数并将其绑定到View
我有一个包含球员和得分的表(姓名:Mike 得分:10,姓名:Peter 得分:5 等)和一个包含 3 张铜牌、银牌和金牌图片的视图。 在每张图片下方,我想显示每周/每轮获胜者的玩家姓名。 很可能有多个铜牌、银牌或金牌。
在我的 TotalViewModel 中,我有一个用于所有总分数的 ObservableCollection:
public const string TotalsPropertyName = "Totals";
private ObservableCollection<TotalViewModel> _totals;
public ObservableCollection<TotalViewModel> Totals
{
get
{
return _totals;
}
set
{
if (_totals == value)
return;
_totals = value;
RaisePropertyChanged(TotalsPropertyName);
}
}
我想使用 IEnumerable,因为有多个数字一、二、三分数:
public IEnumerable<TotalViewModel> FirstOne { get; private set; }
private void UpdateFirstOne()
{
this.FirstOne = this.Totals.Where(elem => elem.Model.score > 0).OrderByDescending(e => e.Model.score);
}
private void Totals_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
try
{
this.UpdateFirstOne();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
我的视图的 DataContext 对于简单的绑定工作正常,它在 UserControl 中设置:
DataContext="{Binding MainTotal, Source={StaticResource Locator}}
但结果没有显示在我的 DataGrid 中(我猜我在这里犯了一个愚蠢的错误):
<data:DataGrid ItemsSource="{Binding FirstOne, Mode=OneWay}" AutoGenerateColumns="true" Height="100" Name="dataGrid1" Width="120" />
所以我有 2 个问题:
- 如何获得所需的 1/2/3 分数集合?
- 如何让它们出现在我的视图中?
谢谢。
I have a table with players and scores (Name:Mike Score:10, Name:Peter Score:5, etc) and a View with 3 pictures of a bronze, silver and gold medal.
Underneath each picture I want to display the playername(s) of the winners each week / round.
Could well be that there are multiple bronze, silver or gold scores.
In my TotalViewModel I have an ObservableCollection for all the Total Scores:
public const string TotalsPropertyName = "Totals";
private ObservableCollection<TotalViewModel> _totals;
public ObservableCollection<TotalViewModel> Totals
{
get
{
return _totals;
}
set
{
if (_totals == value)
return;
_totals = value;
RaisePropertyChanged(TotalsPropertyName);
}
}
I wanted to use a IEnumerable because of the multiple numbers one, two, three score:
public IEnumerable<TotalViewModel> FirstOne { get; private set; }
private void UpdateFirstOne()
{
this.FirstOne = this.Totals.Where(elem => elem.Model.score > 0).OrderByDescending(e => e.Model.score);
}
private void Totals_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
try
{
this.UpdateFirstOne();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
The DataContext of my View is working fine for the straightforward bindings, it's set in the UserControl:
DataContext="{Binding MainTotal, Source={StaticResource Locator}}
But the results aren't displayed in my DataGrid (guess I'm making a stupid mistake here):
<data:DataGrid ItemsSource="{Binding FirstOne, Mode=OneWay}" AutoGenerateColumns="true" Height="100" Name="dataGrid1" Width="120" />
So I have 2 questions:
- How do I get the desired collection of number 1/2/3 scores?
- How do I get them in my View?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我不会进入数据绑定位,但提取前三个的 linq 查询可能是这样的:
Well I wont get into the data binding bit but a linq query to extract the top three could be something like this:
您正在绑定到 FirstOne,因此您需要对该属性进行属性更改。
You're binding to FirstOne so you need to do a propertychanged for that property.