Search code examples
c#wpfdatagridreactiveui

Using a WPF DataGrid with Reactivui


I'm trying to bind a WPF DataGrid to a ReactiveList. Unfortunately the bind don't show any row.

I tried to use a normal List as Datasource as well. I also tried the AutoGenerateColumn function and to use Binding to the properties(only getters, no setters) of AuswertungsEntry. But the properties never gotten accessed at runtime.

My view.xaml:

...

<DataGrid x:Name="AuswertungGrid" Grid.Row="1">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Fehler" Binding="{Binding Name}"></DataGridTextColumn>
        <DataGridTextColumn Header=" 1. Auswertung" Binding="{Binding ok1}"></DataGridTextColumn>
        <DataGridTextColumn Header=" 1. Auswahl"></DataGridTextColumn>
        <DataGridTextColumn Header=" 2. Auswertung"></DataGridTextColumn>
        <DataGridTextColumn Header=" 2. Auswahlt"></DataGridTextColumn>
        <DataGridTextColumn Header=" Gesamt"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

My binding in view.xaml.cs:

this.WhenActivated((d) =>
{                
    this.OneWayBind(ViewModel,
         viewModel => viewModel.Entries,
         view => view.AuswertungGrid.ItemsSource)
         .DisposeWith(d);
}

My ViewModel:

...
 private readonly ObservableAsPropertyHelper<ReactiveList<AuswertungsEntry>> _Entries;
 public ReactiveList<AuswertungsEntry> Entries => _Entries.Value;

 public AuswertungViewModel()
 {
    _Entries = this.WhenAnyValue(x => x.Data)
                   .Where(x => x != null)
                   .SelectMany(x => CreateDataSource())
                   .ToProperty(this, x => x.Entries);
 }

 public async Task<ReactiveList<AuswertungsEntry>> CreateDataSource()
 {
    return await AuswertungsService.GetAuswertung();
 }
...

The Service is returning correct Data. I checked that already. But no rows are getting generated, not with the GenerateAutoColumn feature nor manually.

I would expect, that DataGrid would get filled with the supplied data.


Solution

  • ReactiveList<T> was deprecated a while back within the ReactiveUI project.

    One of the issues is it doesn't produce INotifyCollectionChanged events in a way that WPF always expects, especially when you are adding ranged based values.

    I would recommend you use a SourceList<T> if you need to mutate the data, or a ObservableCollection<T> otherwise.