2016-07-23 93 views
-1

我有一个与一个国家相关的匹配列表。现在,在迭代我给你这个国家的每一场比赛,因此,例如:如何防止ItemCount出现重复?

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中防止这种情况,并且不显示重复的项目,这可能吗?

+2

您没有提供足够的细节。我需要成为诺查丹玛斯才能回答 – Liero

+1

你可以在你设置'ItemCount'的地方发布代码吗? – mariosangiorgio

+0

XAML在这里看起来大体上不相关......它只显示您已经计算出的内容。如何填充列表的C#代码? –

回答

0

你的问题是模糊的,但我猜你根据你的城市名称没有按你的结果。

1

我打算继续,并假设您有一个标有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

this.ItemCount = distinctList.Count();