如何防止ItemCount重复
本文关键字:重复 ItemCount 何防止 | 更新日期: 2023-09-27 17:58:17
我有一个与国家相关的匹配列表。现在在迭代中,我为每一场比赛分配这个国家,例如:
Team A = Italy
Team B = Italy
我在GridView中绑定这些匹配项,如下所示:
<Expander IsExpanded="True" Background="#4F4F4F">
<Expander.Header>
<StackPanel Orientation="Horizontal" Height="22">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Orange" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" match" FontSize="22" Foreground="White" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
不管怎么说,name就是国名,问题是ItemCount抢了意大利两次。我需要在xaml中防止这种情况,并且不显示重复的项目,这可能吗?
我将继续假设您有一个类型为Team
的泛型List
。
试试这个:
List<Team> teams = new List<Team>
{
new Team {Name = "Italy"},
new Team {Name = "France"},
new Team {Name = "Italy"}
};
var distinctList = teams.Select(team => team.Name)
.Distinct()
.Select(team => new Team {Name = team})
.OrderBy(team => team.Name)
.ToList();
要非常小心,因为这不是针对性能进行优化的,这只会给你带来你想要的结果。如果数据集非常大,请不要使用此选项,否则可能需要补偿加载时间。此外,您可能希望在IEqualityComparer
中包含一些类类型,以适应StringComparison
。然后,比较器将作为参数传递给Distinct()
。
至于ItemCount,只要在获得distinctList
后在ViewModel中设置该变量即可。
this.ItemCount = distinctList.Count();
您的问题不明确,但我猜您并没有根据城市名称对结果进行分组。