Search code examples
wpfcaliburn.microsimple-injector

ViewModel and View not getting bound for user control in listbox


I have a BooksListViewModel & BooksListView which have just a list box named Books which should be populated via the BindableCollection<BookViewModel> Books property in the ViewModel.

Nothing is showing up, and I think either the Book listbox and property aren't being properly bound, or it's the BookView and BookViewModel, but I can't figure out which.

I'm also using SimpleInjector, and I think it's that I maybe haven't set that up correctly? I copied most of the code from the Caliburn feature samples - specifically from the Bindings example.

Code for the bootstrapper:

namespace WPFCaliburnUI
{
    public class Bootstrapper : BootstrapperBase
    {
        private Container _container;

        public Bootstrapper()
        {
            Initialize();
        }

        protected override void Configure()
        {
// Mostly taken from here: https://www.c-sharpcorner.com/blogs/migrating-to-simple-injector-30-with-caliburn-micro-bootstrap-changes
            this._container = new Container();
            this._container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();

            this._container.RegisterSingleton<IWindowManager, WindowManager>();
            this._container.RegisterSingleton<IEventAggregator, EventAggregator>();

            this._container.Register<ShellWindowViewModel>();
            this._container.Register<BooksListViewModel>();

            SetupContextAndCrudServices() // Removed this method since it's a little long, 
                                          // but I did verify it is working correctly. 
                                          // Just adds the context and other stuff to the container.

            this._container.Verify();
        }

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<ShellWindowViewModel>();
        }

        protected override IEnumerable<Assembly> SelectAssemblies()
        {
            return new[] { Assembly.GetExecutingAssembly() };
        }

        protected override object GetInstance(Type service, string key)
        {
            return this._container.GetInstance(service);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            IServiceProvider provider = this._container;
            Type collectionType = typeof(IEnumerable<>).MakeGenericType(service);
            var services = (IEnumerable<object>)provider.GetService(collectionType);
            return services ?? Enumerable.Empty<object>();
        }

        protected override void BuildUp(object instance)
        {
            var registration = this._container.GetRegistration(instance.GetType(), true);
            registration.Registration.InitializeInstance(instance);
        }
    }
}

BooksListViewModel:

internal class BooksListViewModel : Screen
{
    private readonly ICrudServices _service;

    private BookViewModel _selectedBook;

    public BookViewModel SelectedBook {
        get => this._selectedBook;
        set => Set(ref this._selectedBook, value);
    }

    public BooksListViewModel(ICrudServices service)
    {
        this._service = service;
        var booksQuery = _service.ReadManyNoTracked<BookListDto>().Take(10);
        var bcBooks = new Collection<BookViewModel>();
        foreach (var bookDto in booksQuery)
        {
            bcBooks.Add(new BookViewModel(bookDto.Title, bookDto.AuthorsOrdered));
        }
        // I did double check that this worked, Books has 10 items.
        this.Books = new BindableCollection<BookViewModel>(bcBooks);
    }

    public BindableCollection<BookViewModel> Books { get; private set; }
}

BooksListView:

<Page
    x:Class="WPFCaliburnUI.Views.BooksListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WPFCaliburnUI.ViewModels"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="BooksListView"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">

    <Grid Name="ContentPanel">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ListBox x:Name="Books">
        </ListBox>

        <StackPanel
            Grid.Row="1"
            Margin="0,12"
            Orientation="Horizontal">
            <TextBlock Margin="0,0,6,0" Text="Selected:" />
            <TextBlock x:Name="SelectedBook_Title" />
        </StackPanel>
    </Grid>
</Page>

BookViewModel:

namespace WPFCaliburnUI.ViewModels
{
    internal class BookViewModel
    {
        public string Title { get; set; }
        public string Author { get; set; }

        public BookViewModel(string title, string author)
        {
            this.Title = title;
            this.Author = author;
        }
    }
}

BookView:

<UserControl
    x:Class="WPFCaliburnUI.Views.BookView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cal="http://www.caliburnproject.org"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WPFCaliburnUI.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DesignHeight="450"
    d:DesignWidth="600"
    mc:Ignorable="d">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Ellipse
            Grid.Column="0"
            Width="48"
            Height="48"
            Margin="0,0,12,0"
            VerticalAlignment="Top"
            Fill="DarkBlue" />

        <StackPanel Grid.Column="1">
            <TextBlock x:Name="Title"
                FontSize="16"
                FontWeight="SemiBold" />
            <TextBlock x:Name="Author" />
        </StackPanel>
    </Grid>
</UserControl>

Edit: Some people asked for how BooksListViewModel is called, so here is the code from the ShellWindowViewModel

namespace WPFCaliburnUI.ViewModels
{
    internal class ShellWindowViewModel : Screen
    {
        private readonly Container _container;
        private readonly IWindowManager _windowManager;
        private INavigationService navigationService;

        public ShellWindowViewModel(Container container, IWindowManager windowManager)
        {
            this._container = container;
            this._windowManager = windowManager;
        }

        public void RegisterFrame(Frame frame)
        {
            navigationService = new FrameAdapter(frame);

            navigationService.NavigateToViewModel(typeof(BooksListViewModel));
        }
    }
}

Solution

  • Thanks to @JackHughes I was able to fix it. ViewModels must be public, not internal. Switching everything to public fixed it.