【问题标题】:Using a WPF DataGrid with Reactivui将 WPF DataGrid 与 Reactivui 一起使用
【发布时间】:2019-06-19 08:33:33
【问题描述】:

我正在尝试将 WPF DataGrid 绑定到 ReactiveList。不幸的是,绑定不显示任何行。

我也尝试使用普通列表作为数据源。 我还尝试了 AutoGenerateColumn 函数并使用绑定到 AuswertungsEntry 的属性(只有 getter,没有 setter)。 但是这些属性在运行时从未被访问过。

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

我在 view.xaml.cs 中的绑定:

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

我的视图模型:

...
 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();
 }
...

服务正在返回正确的数据。我已经检查过了。但是没有生成行,不是使用 GenerateAutoColumn 功能也不是手动生成的。

我希望 DataGrid 会被提供的数据填充。

【问题讨论】:

  • 您确认CreateDataSource 被调用了吗?请提供您的问题的minimal repo
  • 我们弃用了 ReactiveList,它有时在这种情况下效果不佳。考虑使用 ObservableCollection 或 DynamicData 中的 SourceList。
  • ReactiveList 可能没有正确支持 INotifyCollectionChanged 事件,它可以在一个事件中发送范围内的所有值。因此,它被弃用的原因之一是因为很多 ItemsControl 不支持范围值。
  • @GlennWatson 感谢您的支持,我将我的 DataSource 更改为 SourceList 并且它正在工作。如果您愿意,可以将您的评论更改为答案,以便我将其标记为已解决。
  • @mm8 感谢您的意见。在我将我的 DataSource 更改为 SourceList 后,它起作用了

标签: c# wpf datagrid reactiveui


【解决方案1】:

ReactiveList&lt;T&gt; 在 ReactiveUI 项目中被弃用了一段时间。

其中一个问题是它不会以 WPF 始终期望的方式生成 INotifyCollectionChanged 事件,尤其是当您添加基于范围的值时。

如果您需要更改数据,我建议您使用 SourceList&lt;T&gt;,否则使用 ObservableCollection&lt;T&gt;

【讨论】:

    猜你喜欢
    • 2011-06-03
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2015-08-27
    • 2021-05-28
    相关资源
    最近更新 更多