C# ListCollectionView组描述问题
我有一个名为ContactList的类,它有一个名为AggoLabels的属性,这是一个可观察的集合。当ContactList被填充时,Aggregatables集合将包含一些重复的AggregatedLabel。有没有办法使用ListCollectionView按名称对这些AggregatedLabel进行分组,以便在我将集合绑定到WPF中的listBox时不会显示重复项?按ContactListName列出的代码段组中的代码有什么方法可以修改它以实现我的目标吗?谢谢 联系人名单 代码片段C# ListCollectionView组描述问题,c#,wpf,C#,Wpf,我有一个名为ContactList的类,它有一个名为AggoLabels的属性,这是一个可观察的集合。当ContactList被填充时,Aggregatables集合将包含一些重复的AggregatedLabel。有没有办法使用ListCollectionView按名称对这些AggregatedLabel进行分组,以便在我将集合绑定到WPF中的listBox时不会显示重复项?按ContactListName列出的代码段组中的代码有什么方法可以修改它以实现我的目标吗?谢谢 联系人名单 代码片段 由
由于您尚未提供有关Aggregatables使用的信息,我希望此解决方案将有助于:
<ComboBox Name="contactsListBox" ItemsSource="{Binding MyList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ContactListName}"/>
<ComboBox ItemsSource="{Binding AggLabels, Converter={StaticResource Conv}}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public class AggLabelsDistictConverter : IValueConverter
{
class AggregatedLabelComparer : IEqualityComparer<AggregatedLabel>
{
#region IEqualityComparer<AggregatedLabel> Members
public bool Equals(AggregatedLabel x, AggregatedLabel y)
{
return x.Name == y.Name;
}
public int GetHashCode(AggregatedLabel obj)
{
return obj.Name.GetHashCode();
}
#endregion
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is IEnumerable<AggregatedLabel>)
{
var list = (IEnumerable<AggregatedLabel>)value;
return list.Distinct(new AggregatedLabelComparer());
}}}
正如你们所看到的,我以通常的方式绑定ContactListEnumerable,并在每个ContactList容器中聚集标签列表——通过移除重复项的转换器 您能否用ContactList、Aggatables和您期望的输出中的具体值来说明您的示例?是的,因此我的ContactListID和ContactListName始终是唯一的,Aggatables具有唯一的ID,但名称可以重复。例如,对于一个ContactList对象,它可以是ContactListID=01,ContactListName=List1,然后Aggregatel集合可以包含ID=01和Name=Aggregat贝尔,ID=02和Name=Aggregat贝尔,ID=03和Name=NewAggregat贝尔。希望这有意义吗?事实上没有,您询问了Aggregatebles中的非重复值,但我看不到Aggregatebles的用法,即在哪里有重复的Aggregatebel。在您的代码片段中,按ContactListName执行groupping,这是唯一的,我假设您在其他地方有ComboBoxItem DataTemplate,它包含重复的聚合标签。我说得对吗?是的,没错。是否有更好的方法确保我的DataTemplate不显示重复的Aggregatel.Names?
public class AggregatedLabel
{
public int ID { get; set; }
public string Name { get; set; }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//TODO: Add event handler implementation here.
ListCollectionView lcv = new ListCollectionView(myContactLists);
lcv.GroupDescriptions.Add(new PropertyGroupDescription("ContactListName"));
contactsListBox.ItemsSource = lcv.Groups;
}
<ComboBox Name="contactsListBox" ItemsSource="{Binding MyList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ContactListName}"/>
<ComboBox ItemsSource="{Binding AggLabels, Converter={StaticResource Conv}}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public class AggLabelsDistictConverter : IValueConverter
{
class AggregatedLabelComparer : IEqualityComparer<AggregatedLabel>
{
#region IEqualityComparer<AggregatedLabel> Members
public bool Equals(AggregatedLabel x, AggregatedLabel y)
{
return x.Name == y.Name;
}
public int GetHashCode(AggregatedLabel obj)
{
return obj.Name.GetHashCode();
}
#endregion
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is IEnumerable<AggregatedLabel>)
{
var list = (IEnumerable<AggregatedLabel>)value;
return list.Distinct(new AggregatedLabelComparer());
}}}