c# - DynamicData - 如何绑定到分组数据
问题描述
我正在使用来自 Roland Pheasant 的 DynamicData。
我想将我的普通 C# 集合转换为 Rx。
从
ObservableCollection<Grouping<string, DisplayItem>>
转动态数据格式
ReadOnlyObservableCollection<IGroup<DisplayItem, string>>
或者
ReadOnlyObservableCollection<IGroup<DisplayItem, string, string>>
对于缓存的源。
目前我的 xaml 将如下所示以正常绑定ObservableCollection<Grouping<string, DisplayItem>>
<CollectionViewSource
x:Name="GroupedDataCollection"
Source="{x:Bind ViewModel.bindingData_grouped, Mode=OneWay}"
IsSourceGrouped="True" />
<ListView
Margin="16,0"
ItemsSource="{x:Bind GroupedDataCollection.View , Mode=OneWay}"
SelectionMode="None">
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="False">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontSize="14"
FontWeight="SemiBold"
Text="{Binding Key }" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate >
<DataTemplate x:DataType="viewmodels:DisplayItem">
<StackPanel>
<TextBlock FontSize="14"
FontWeight="SemiBold"
Text="{x:Bind Description }" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
模型 :
public class DisplayItem {
public string ID { get; set; } = Guid.NewGuid().ToString();
public string Type { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public double? Price { get; set; } = 0; }
现有的 c# Observable Collection 结果:
动态数据代码:
public ReadOnlyObservableCollection<IGroup<DisplayItem, string>> bindingData_grouped;
var myBindingOperation_grouped = Data.ToObservableChangeSet()
.GroupOn(x => x.Type)
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(out bindingData_grouped)
.Subscribe();
如果我绑定到ReadOnlyObservableCollection<IGroup<DisplayItem, string>>
或ReadOnlyObservableCollection<IGroup<DisplayItem, string, string>>
我的列表视图不显示任何内容,则使用上面的代码。
如何使用“CollectionViewSource”从 xaml listview 绑定到
ReadOnlyObservableCollection<IGroup<DisplayItem, string>>
或
ReadOnlyObservableCollection<IGroup<DisplayItem, string, string>>
提前谢谢。
解决方案
我是 DynamicData 的新手,所以我可能会重新实现库中已经存在的东西,但这就是我想出的:
// custom IGrouping implementation which is required by ListView
public class Grouping<TKey, TElement> : ObservableCollectionExtended<TElement>, IGrouping<TKey, TElement>
{
public Grouping(IGroup<TElement, TKey> group)
{
if (group == null)
{
throw new ArgumentNullException(nameof(group));
}
Key = group.GroupKey;
group.List.Connect().Bind(this).Subscribe();
}
public TKey Key { get; private set; }
}
// this.Ints is an ObservableCollection<int> which I manipulate thru UI
this.Ints
.ToObservableChangeSet()
.GroupOn(i => (int)(i / 10)) // group by 10s
.Transform(group => new Grouping<int, int>(group)) // transform DynamicData IGroup into our IGrouping implementation
.Sort(SortExpressionComparer<Grouping<int, int>>.Ascending(t => t.Key)) // sort by keys
.ObserveOnDispatcher()
.Bind(this.GroupedInts) // this.GroupedInts is used as binding source for CollectionViewSource in UI
.Subscribe();
private ObservableCollectionExtended<Grouping<int, int>> GroupedInts { get; } = new ObservableCollectionExtended<Grouping<int, int>>();
基本上,您需要 ListView (CollectionViewSource) 分组机制所需的 IGrouping 接口的自定义实现。您将一个 IGroup 实例(来自 DynamicData 的GroupOn
方法)传递给它。在构造函数中,您将 Connect() 连接到组的 ReactiveList 并将其绑定到Grouping
自身。
推荐阅读
- facebook - 无法在 Facebook 上运行简单的应用程序托管
- javascript - html中大小为0的下拉列表的滚动条
- c# - 如何正确反序列化统一 JsonUtility.fromJson() 方法的字符串
- android - 在 Android 中创建 Web 包装器时 VideoCapabilities 出错
- sql-server - SQL STRING_SPLIT 不起作用
- ant - 在 ant 中,如何创建和使用目标“库”?
- api - Gitlab项目API查询
- jquery - 选择2
- java - 不明白在处理 3 中访问 CSV 文件的工作原理。需要解释
- c# - 在 Entity Framework Core 中获取与条件的关系