使用LINQ获取1-2-3分数并将其绑定到View

发布于 2024-11-30 19:17:41 字数 1542 浏览 1 评论 0原文

我有一个包含球员和得分的表(姓名: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. 如何获得所需的 1/2/3 分数集合?
  2. 如何让它们出现在我的视图中?

谢谢。

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:

  1. How do I get the desired collection of number 1/2/3 scores?
  2. How do I get them in my View?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄与心 2024-12-07 19:17:41

好吧,我不会进入数据绑定位,但提取前三个的 linq 查询可能是这样的:

var topThreeScoringNameLists =
    Totals
        .GroupBy(x => x.Model.Score)
        .OrderByDescending(x => x.Key)
        .Take(3)
        .Select(x => 
            String.Join(", ", x.Select(y => y.Model.Name).ToArray()));

Well I wont get into the data binding bit but a linq query to extract the top three could be something like this:

var topThreeScoringNameLists =
    Totals
        .GroupBy(x => x.Model.Score)
        .OrderByDescending(x => x.Key)
        .Take(3)
        .Select(x => 
            String.Join(", ", x.Select(y => y.Model.Name).ToArray()));
温柔一刀 2024-12-07 19:17:41

您正在绑定到 FirstOne,因此您需要对该属性进行属性更改。

public IEnumerable<TotalViewModel> FirstOne 
{ 
    get
    {
        return _firstOne;
    }
    private set
    {
        _firstOne = value;
        RaisePropertyChanged("FirstOne");
    }
}

You're binding to FirstOne so you need to do a propertychanged for that property.

public IEnumerable<TotalViewModel> FirstOne 
{ 
    get
    {
        return _firstOne;
    }
    private set
    {
        _firstOne = value;
        RaisePropertyChanged("FirstOne");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文