-
Hello! I've been pulling my hair out with this problem. I'd like to use WhenActivated in the view model and view's code-behind and bind a collection to a combobox's itemssource. I cannot get this to work when I use WhenActivated in the view's code-behind, but it works when I use bindings from xaml. Full reproducible project here: https://github.com/FaithBeam/AvaloniaWhenActivatedCollectionBinding MainWindowViewModel.cs: using System;
using System.Collections.ObjectModel;
using System.Reactive.Disposables;
using DynamicData;
using ReactiveUI;
namespace AvaloniaWhenActivatedCollectionBinding.ViewModels;
public partial class MainWindowViewModel : ViewModelBase, IActivatableViewModel
{
private readonly ReadOnlyObservableCollection<string> _strings;
public MainWindowViewModel()
{
Activator = new ViewModelActivator();
var sourceList = new SourceList<string>();
sourceList.Connect().Bind(out _strings).Subscribe();
this.WhenActivated(d =>
{
sourceList.Edit(x => x.Add("First"));
this.RaisePropertyChanged(nameof(Strings));
Disposable.Create(() => { }).DisposeWith(d);
});
}
public ReadOnlyObservableCollection<string> Strings => _strings;
public ViewModelActivator Activator { get; }
} MainWindow.xaml.cs: using System.Reactive.Disposables;
using Avalonia.Controls;
using Avalonia.ReactiveUI;
using AvaloniaWhenActivatedCollectionBinding.ViewModels;
using ReactiveUI;
namespace AvaloniaWhenActivatedCollectionBinding.Views;
public partial class MainWindow : ReactiveWindow<MainWindowViewModel>
{
public MainWindow()
{
InitializeComponent();
this.WhenActivated(d =>
{
this.OneWayBind(ViewModel, vm => vm.Strings, v => v.ComboBox.ItemsSource).DisposeWith(d);
});
}
} The combobox has an item, but there's no text... And once again, when I comment out the code-behind's when activated block and bind the combobox's items source in xaml, it works, but I'd like to use the code-behind :). Xaml binding works for reference: Any ideas? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Does it work if you use a dispatcher call? From memory ReactiveUI uses its own scheduler thread. But this may be a different problem entirely. |
Beta Was this translation helpful? Give feedback.
I had a look and it is binding properly, but the ComboBox is failing to find a DataTemplate for the new child and so not presenting anything. If you manually set a DataTemplate like below everything shows up fine. I'm not sure why it's not falling back on the default template.
There are multiple errors logged without the template.
DefaultViewLocator: Failed to resolve view for view model type 'System.Object'.
ViewModelViewHost: Couldn't find view for 'First'. Is it registered? Falling back to default content.
More Digging:
Avalonia implements a BindingHook that auto assigns t…