DynamicData-如何绑定到分组数据

时间:2018-08-30 10:17:29

标签: c# system.reactive observablecollection dynamic-data

我正在使用Roland Pheasant的DynamicData。

DynamicData

我想将普通的C#集合转换为Rx。

来自 ObservableCollection<Grouping<string, DisplayItem>>

转换为DynamicData格式

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#可观察的采集结果: Existing results

动态数据代码:

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>>

提前想。

1 个答案:

答案 0 :(得分:1)

我是DynamicData的新手,所以我可能会重新实现lib中已经存在的某些东西,但这是我想到的:

// 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本身。