WPF中是否可以选择Datagrid分组顺序?
我刚刚开始学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先回答你的最后一个问题:) OrderBy 在这里没有任何意义,因为在你对球员进行排序后,你将列表放入 CollectionView 中,并进行分组,该分组会立即按团队对其进行排序(因为分组是在团队上)。
为了让它们按正确的顺序排列,您可以先按团队对它们进行排序,然后再将它们放入 ListCollectionView 中。但是,这会将它们按字母顺序排列 - 而不是您想要的顺序。您需要实现 IComparable 并将其放入您的排序方法中 - 我稍微重写了您的方法(我不太擅长 Linq 形式 - 所以,请耐心等待:)):
您让自己变得有点困难通过引入一些不必要的东西 - 我试图将它们根除。
希望这有帮助!
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.
Hope this helps!