WPF中是否可以选择Datagrid分组顺序?

发布于 2024-09-16 04:10:10 字数 1293 浏览 1 评论 0原文

我刚刚开始学习 WPF/C#,我选择了一个对我实际上有用的项目,而不是“Hello, World!”的变体。程序。

它是一个小型应用程序,用于轮询游戏服务器以获取玩家信息并将其绑定到 DataGrid。数据按团队分组,可以是四个值之一:蓝色、红色、观众或无。

我的 Linq 查询工作正常,DataGrid 分组也几乎很好,除了一个小问题:四个团队组的顺序每次都不同。有时红色是第一个,有时是无,等等。

有什么方法可以强制各组按上面的顺序吗?

这是 Linq 查询(addr 是服务器 ip):

private void get_server(string addr)
    {
        var loc = "http://ukcs.gameme.com/api/serverinfo/" + addr + "//players";
        XDocument doc = XDocument.Load(@loc);

        var server = new ObservableCollection<Player>
            (from player in doc.Descendants("player")
            orderby player.Element("rank").Value
            select new Player
            {
                name = player.Element("name").Value,
                team = player.Element("team").Value,
                frags = player.Element("kills").Value + ":" + player.Element("deaths").Value,
                rank = int.Parse(player.Element("rank").Value)
            });

        server.OrderBy(p => p.rank);
        ListCollectionView collection = new ListCollectionView(server);
        collection.GroupDescriptions.Add(new PropertyGroupDescription("team"));
        player_list.ItemsSource = collection;
    }

第二个问题是两个 OrderBy 似乎都没有效果。

任何帮助将不胜感激!

I've just started to learn WPF/C# and I picked a project which would actually be useful to me instead of variations of "Hello, World!" programs.

It's a small app that polls a game server for player information and binds it to a DataGrid. The data is grouped by team, which can be one of four values: Blue, Red, Spectator, or None.

I've got the Linq query working fine, and the DataGrid grouping is almost good, except for one small problem: the order of the four teams groups is different every time. Sometimes Red is first, sometimes None, etc.

Is there any way I can force the groups into the order above?

Here is the Linq query (addr is the server ip):

private void get_server(string addr)
    {
        var loc = "http://ukcs.gameme.com/api/serverinfo/" + addr + "//players";
        XDocument doc = XDocument.Load(@loc);

        var server = new ObservableCollection<Player>
            (from player in doc.Descendants("player")
            orderby player.Element("rank").Value
            select new Player
            {
                name = player.Element("name").Value,
                team = player.Element("team").Value,
                frags = player.Element("kills").Value + ":" + player.Element("deaths").Value,
                rank = int.Parse(player.Element("rank").Value)
            });

        server.OrderBy(p => p.rank);
        ListCollectionView collection = new ListCollectionView(server);
        collection.GroupDescriptions.Add(new PropertyGroupDescription("team"));
        player_list.ItemsSource = collection;
    }

A second problem is that neither of the OrderBys seem to have an effect.

Any help would be appreciated!

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

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

发布评论

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

评论(1

尤怨 2024-09-23 04:10:10

首先回答你的最后一个问题:) OrderBy 在这里没有任何意义,因为在你对球员进行排序后,你将列表放入 CollectionView 中,并进行分组,该分组会立即按团队对其进行排序(因为分组是在团队上)。

为了让它们按正确的顺序排列,您可以先按团队对它们进行排序,然后再将它们放入 ListCollectionView 中。但是,这会将它们按字母顺序排列 - 而不是您想要的顺序。您需要实现 IComparable 并将其放入您的排序方法中 - 我稍微重写了您的方法(我不太擅长 Linq 形式 - 所以,请耐心等待:)):

您让自己变得有点困难通过引入一些不必要的东西 - 我试图将它们根除。

private void get_server(string addr)
{
   var loc = "http://ukcs.gameme.com/api/serverinfo/" + addr + "//players";
   var doc = XDocument.Load(@loc);

   var server = doc.Descendants("player")
            .Select(player => new Player
                                  {
                                      name =player.Element("name").Value,
                                      team=player.Element("team").Value,
                                      frags=player.Element("kills").Value +":" +player.Element("deaths").Value,
                                      rank=int.Parse(player.Element("rank").Value)
                                  })
            .OrderBy(p => p.team,new CustomSort())
            .ThenBy(p => p.rank).ToList();
    var collection = new ListCollectionView(server);
    collection.GroupDescriptions.Add(new PropertyGroupDescription("team"));
    player_list.ItemsSource = collection;
}
public class CustomSort : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x.Equals(y))
            return 0;
        if (y.Equals("None") || x.Equals("Blue"))
            return 1;
        if (x.Equals("None") || y.Equals("Blue"))
            return -1;
        if (x.Equals("Red")|| y.Equals("Spectator"))
            return -1;
        return 1; // y == "Red" and x == "Spectator"
    }
}

希望这有帮助!

To answer your last question first :) The OrderBy's have no meaning here, because after you sort the players, you put the list inside a CollectionView with a grouping which promptly sorts it by Team (since the grouping is on Team).

To get them in the proper order, you can sort them by Team before putting them inside the ListCollectionView. However, this will put them in alphabetical order - and not exactly the order you wanted. You need to implement IComparable and put that in your sort method - i've rewritten your method a bit (I am not so good at that Linq-form - so, bear with me :)):

You made it a bit harder on yourself by introducing a few unnecessary things - I've tried to root them out.

private void get_server(string addr)
{
   var loc = "http://ukcs.gameme.com/api/serverinfo/" + addr + "//players";
   var doc = XDocument.Load(@loc);

   var server = doc.Descendants("player")
            .Select(player => new Player
                                  {
                                      name =player.Element("name").Value,
                                      team=player.Element("team").Value,
                                      frags=player.Element("kills").Value +":" +player.Element("deaths").Value,
                                      rank=int.Parse(player.Element("rank").Value)
                                  })
            .OrderBy(p => p.team,new CustomSort())
            .ThenBy(p => p.rank).ToList();
    var collection = new ListCollectionView(server);
    collection.GroupDescriptions.Add(new PropertyGroupDescription("team"));
    player_list.ItemsSource = collection;
}
public class CustomSort : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x.Equals(y))
            return 0;
        if (y.Equals("None") || x.Equals("Blue"))
            return 1;
        if (x.Equals("None") || y.Equals("Blue"))
            return -1;
        if (x.Equals("Red")|| y.Equals("Spectator"))
            return -1;
        return 1; // y == "Red" and x == "Spectator"
    }
}

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文