首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListCollectionView GroupDescription问题

ListCollectionView GroupDescription问题
EN

Stack Overflow用户
提问于 2011-01-24 20:42:44
回答 1查看 1.7K关注 0票数 0

我有一个名为ContactList的类,它有一个名为AggLabels的属性,它是一个可观察的集合。当ContactList被填充时,AggLabels集合将包含一些重复的AggregatedLabels。是否有一种方法可以使用AggregatedLabels按“名称”对这些ListCollectionView进行分组,以便在将集合绑定到WPF中的listBox时不会显示重复项?ContactListName在我的代码片段组中有什么方法可以修改这个代码以实现我的目标吗?谢谢

ContactList

代码语言:javascript
复制
public class ContactList
{           
    public int ContactListID { get; set; }
    public string ContactListName { get; set; }
    public ObservableCollection<AggregatedLabel> AggLabels { get; set; }
}

AggregatedLabel

代码语言:javascript
复制
public class AggregatedLabel
{
    public int ID { get; set; }
    public string Name { get; set; }

}

代码片段

代码语言:javascript
复制
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;

    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-25 06:24:26

由于您尚未提供有关AggLabels使用的信息,我希望此解决方案将有助于:

代码语言:javascript
复制
    <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列表-通过转换器删除重复。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4786957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档