我正在使用 Roland Pheasant 的动态数据。
我想将我的普通 C# 集合转换为 Rx。
来自
ObservableCollection<Grouping<string, DisplayItem>>
动态数据格式
ReadOnlyObservableCollection<IGroup<DisplayItem, string>> // or
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;
}
动态数据代码:
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>> // or
ReadOnlyObservableCollection<IGroup<DisplayItem, string, string>>
我的列表视图什么也没有显示。
如何使用“CollectionViewSource”从 XAML ListView 绑定到
ReadOnlyObservableCollection<IGroup<DisplayItem, string>> // or
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
本身。