我有一个名为ContactList的类,它有一个名为AggLabels的属性,它是一个可观察的集合。当ContactList被填充时,AggLabels集合将包含一些重复的AggregatedLabels。是否有一种方法可以使用AggregatedLabels按“名称”对这些ListCollectionView进行分组,以便在将集合绑定到WPF中的listBox时不会显示重复项?ContactListName在我的代码片段组中有什么方法可以修改这个代码以实现我的目标吗?谢谢
ContactList
public class ContactList
{
public int ContactListID { get; set; }
public string ContactListName { get; set; }
public ObservableCollection<AggregatedLabel> AggLabels { get; set; }
}AggregatedLabel
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;
}发布于 2011-01-25 06:24:26
由于您尚未提供有关AggLabels使用的信息,我希望此解决方案将有助于:
<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容器中绑定AggLabels列表-通过转换器删除重复。
https://stackoverflow.com/questions/4786957
复制相似问题