gpt4 book ai didi

c# - 如何将 WPF CollectionViewGroup 类型更改为自定义类型并在 ListCollectionView 中使用它

转载 作者:行者123 更新时间:2023-11-30 14:10:49 24 4
gpt4 key购买 nike

如何将 View 属性的类型覆盖为我的自定义类型。我的 CustomGroupListCollectionView 类型向 Groups 属性添加了额外的属性。在运行时,当我观察到 View 属性的类型是 ListCollectionView 时,我想将其更改为 CustomGroupListCollectionView。

public class CollectionViewSourceCustom : CollectionViewSource
{
public new CustomGroupListCollectionView View { get; set; }
}

public class CustomGroupListCollectionView : ListCollectionView
{
private readonly CustomGroup _allGroup;

public CustomGroupListCollectionView(IList list)
: base(list)
{
_allGroup = new CustomGroup("All");
foreach (var item in list)
{
_allGroup.AddItem(item);
}
}

public override ReadOnlyObservableCollection<object> Groups
{
get
{
var group = new ObservableCollection<object>(base.Groups.ToList());
group.Add(_allGroup);
return new ReadOnlyObservableCollection<object>(group);
}
}
}

public class CustomGroup : CollectionViewGroup
{
public CustomGroup(object name)
: base(name)
{
}

public void AddItem(object item)
{
ProtectedItems.Add(item);
}

public override bool IsBottomLevel
{
get { return true; }
}

bool _IsChecked;
public bool IsChecked
{
get { return _IsChecked; }
set { _IsChecked = value; }
}
Visibility _CheckBoxVisibility;
public Visibility CheckBoxVisibility
{
get { return _CheckBoxVisibility; }
set { _CheckBoxVisibility = value; }
}

bool _IsExpanded;
public bool IsExpanded
{
get { return _IsExpanded; }
set { _IsExpanded = value; }
}
Visibility _ExpanderVisibility;
public Visibility ExpanderVisibility
{
get { return _ExpanderVisibility; }
set { _ExpanderVisibility = value; }
}

Visibility _ImageVisibility = Visibility.Collapsed;
public Visibility ImageVisibility
{
get { return _ImageVisibility; }
set { _ImageVisibility = value; }
}
}

最佳答案

CollectionViewSource 有一个 CollectionViewType 属性,您可以使用它来确定 CollectionViewSource 返回的 CollectionView 的类型,例如

<CollectionViewSource x:Key="source" CollectionViewType="{x:Type my:CustomGroupListCollectionView}" Source="{Binding MyData}"/>

如您所见,您甚至不必创建新的 CollectionViewSource 类。

关于c# - 如何将 WPF CollectionViewGroup 类型更改为自定义类型并在 ListCollectionView 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22341977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com