2011-01-24 110 views
0

我有一个名为ContactList的类,它有一个名为AggLabels的属性,它是一个Observable集合。当一个ContactList被填充时,AggLabels集合将包含一些重复的AggregatedLabels。有没有办法使用ListCollectionView通过“名称”将这些AggregatedLabels分组,以便在将集合绑定到WPF中的列表框时不显示重复项?在我的代码片段组中的ContactListName有没有一种方法可以修改这个来实现我的目标?由于ListCollectionView GroupDescription issue

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; 

    } 
+0

你能说清楚你的例子具体值在ContactList,AggLables和输出你期待? – 2011-01-24 21:02:40

回答

0

你有没有约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列表 - 通过转换器,删除重复。