WPF ReactiveUI 绑定 Listbox.ItemsSource 到 ReadOnlyObservableCollection 不起作用
WPF ReactiveUI binding Listbox.ItemsSource to ReadOnlyObservableCollection not working
在我的 WPF+ReactiveUI+DynamicData 项目中,我无法将 ListBox.ItemsSource 绑定到 ReadOnlyObservableCollection 工作。
我成功地通过后端服务将数据填充到我的 ViewModel
public class RecordingsListViewModel: ReactiveObject
{
private readonly ReadOnlyObservableCollection<RecordingViewModel> _recordings;
public ReadOnlyObservableCollection<RecordingViewModel> Recordings => _recordings;
public RecordingsListViewModel(IRecordingsService recordindsService)
{
recordindsService.RecordingsList.Connect()
.Transform(rec => new RecordingViewModel(rec as IRecordingModel))
.Bind(out _recordings)
.Subscribe();
}
}
我的看法:
<reactiveui:ReactiveUserControl x:Class="MyProject.Views.RecordingsListView"
x:TypeArguments="vm:RecordingsListViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:MyProject.ViewModels"
xmlns:reactiveui="http://reactiveui.net"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ListBox x:Name ="RecordingsListBox" HorizontalContentAlignment="Stretch"/>
</reactiveui:ReactiveUserControl>
在我后面的代码中,我尝试绑定但它不起作用
{
public static readonly DependencyProperty ViewModelProperty = DependencyProperty
.Register(nameof(ViewModel), typeof(RecordingsListViewModel), typeof(RecordingsListView));
public RecordingsListViewModel ViewModel
{
get => (RecordingsListViewModel)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
object IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (RecordingsListViewModel)value;
}
public RecordingsListView()
{
ViewModel = Locator.Current.GetService<RecordingsListViewModel>();
InitializeComponent();
this.WhenActivated(d =>
{
this.OneWayBind(ViewModel,
vm => ViewModel.Recordings,
view => view.RecordingsListBox.ItemsSource).DisposeWith(d);
});
}
}
输出有错误,但不会导致应用程序崩溃
PropertyBinderImplementation: view.RecordingsListBox.ItemsSource Binding received an Exception! - System.NotSupportedException: Unsupported expression of type 'Constant'. Did you miss the member access prefix in the expression?
at ReactiveUI.ExpressionMixins.GetExpressionChain(Expression expression) in /_/src/ReactiveUI/Mixins/ExpressionMixins.cs:line 78
at ReactiveUI.ReactiveNotifyPropertyChangedMixin.SubscribeToExpressionChain[TSender,TValue](TSender source, Expression expression, Boolean beforeChange, Boolean skipInitial, Boolean suppressWarnings) in /_/src/ReactiveUI/Mixins/ReactiveNotifyPropertyChangedMixin.cs:line 140
at ReactiveUI.WhenAnyMixin.WhenAnyDynamic[TSender,TRet](TSender sender, Expression property1, Func`2 selector) in /_/src/ReactiveUI/VariadicTemplates.cs:line 99
at ReactiveUI.Reflection.<>c__DisplayClass15_0`2.<ViewModelWhenAnyValue>b__2(Object x) in /_/src/ReactiveUI/Expression/Reflection.cs:line 407
at System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Select.cs:line 39
我在谷歌上搜索这个错误和相关示例将近 16 个小时,尝试了很多代码调整但没有任何运气。
非常感谢任何想法
非常感谢 Glenn Watson 和
Đøharrrck 评论。我终于修好了。
问题是在绑定中错误地访问了 viewModel。
是:
this.OneWayBind(ViewModel,
vm => ViewModel.Recordings,
view => view.RecordingsListBox.ItemsSource).DisposeWith(d);
应该是:
this.OneWayBind(ViewModel,
vm => vm.Recordings,
view => view.RecordingsListBox.ItemsSource).DisposeWith(d);
在我的 WPF+ReactiveUI+DynamicData 项目中,我无法将 ListBox.ItemsSource 绑定到 ReadOnlyObservableCollection 工作。 我成功地通过后端服务将数据填充到我的 ViewModel
public class RecordingsListViewModel: ReactiveObject
{
private readonly ReadOnlyObservableCollection<RecordingViewModel> _recordings;
public ReadOnlyObservableCollection<RecordingViewModel> Recordings => _recordings;
public RecordingsListViewModel(IRecordingsService recordindsService)
{
recordindsService.RecordingsList.Connect()
.Transform(rec => new RecordingViewModel(rec as IRecordingModel))
.Bind(out _recordings)
.Subscribe();
}
}
我的看法:
<reactiveui:ReactiveUserControl x:Class="MyProject.Views.RecordingsListView"
x:TypeArguments="vm:RecordingsListViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:MyProject.ViewModels"
xmlns:reactiveui="http://reactiveui.net"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ListBox x:Name ="RecordingsListBox" HorizontalContentAlignment="Stretch"/>
</reactiveui:ReactiveUserControl>
在我后面的代码中,我尝试绑定但它不起作用
{
public static readonly DependencyProperty ViewModelProperty = DependencyProperty
.Register(nameof(ViewModel), typeof(RecordingsListViewModel), typeof(RecordingsListView));
public RecordingsListViewModel ViewModel
{
get => (RecordingsListViewModel)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
object IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (RecordingsListViewModel)value;
}
public RecordingsListView()
{
ViewModel = Locator.Current.GetService<RecordingsListViewModel>();
InitializeComponent();
this.WhenActivated(d =>
{
this.OneWayBind(ViewModel,
vm => ViewModel.Recordings,
view => view.RecordingsListBox.ItemsSource).DisposeWith(d);
});
}
}
输出有错误,但不会导致应用程序崩溃
PropertyBinderImplementation: view.RecordingsListBox.ItemsSource Binding received an Exception! - System.NotSupportedException: Unsupported expression of type 'Constant'. Did you miss the member access prefix in the expression?
at ReactiveUI.ExpressionMixins.GetExpressionChain(Expression expression) in /_/src/ReactiveUI/Mixins/ExpressionMixins.cs:line 78
at ReactiveUI.ReactiveNotifyPropertyChangedMixin.SubscribeToExpressionChain[TSender,TValue](TSender source, Expression expression, Boolean beforeChange, Boolean skipInitial, Boolean suppressWarnings) in /_/src/ReactiveUI/Mixins/ReactiveNotifyPropertyChangedMixin.cs:line 140
at ReactiveUI.WhenAnyMixin.WhenAnyDynamic[TSender,TRet](TSender sender, Expression property1, Func`2 selector) in /_/src/ReactiveUI/VariadicTemplates.cs:line 99
at ReactiveUI.Reflection.<>c__DisplayClass15_0`2.<ViewModelWhenAnyValue>b__2(Object x) in /_/src/ReactiveUI/Expression/Reflection.cs:line 407
at System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Select.cs:line 39
我在谷歌上搜索这个错误和相关示例将近 16 个小时,尝试了很多代码调整但没有任何运气。 非常感谢任何想法
非常感谢 Glenn Watson 和
Đøharrrck 评论。我终于修好了。
问题是在绑定中错误地访问了 viewModel。
是:
this.OneWayBind(ViewModel,
vm => ViewModel.Recordings,
view => view.RecordingsListBox.ItemsSource).DisposeWith(d);
应该是:
this.OneWayBind(ViewModel,
vm => vm.Recordings,
view => view.RecordingsListBox.ItemsSource).DisposeWith(d);