ViewModel和View未绑定到列表框中的用户控件

时间:2018-10-04 17:09:47

标签: wpf caliburn.micro simple-injector

我有一个BooksListViewModelBooksListView,它们只有一个名为Books的列表框,应该通过ViewModel的BindableCollection<BookViewModel> Books属性来填充。

没有任何显示,我认为“书”列表框和属性未正确绑定,或者是BookViewBookViewModel,但我不知道是哪个。

我也在使用SimpleInjector,我想可能是我没有正确设置?我从Caliburn功能示例中复制了大部分代码,特别是从Bindings示例中复制了代码。

引导程序的代码:

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>

编辑: 有人问如何调用BooksListViewModel,所以这是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));
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我相信@Nkosi的“手动”含义更像是在后面的代码中创建视图模型的新实例并设置为DataContext或从XAML端创建手动绑定。如果您知道一般如何使用依赖项注入和IoC容器,那么我想您知道如何以正确的方式设置datacontext,那么请告诉我,我将为您提供帮助。无论如何,如果我在你的位置,我会在xaml中设置绑定,例如:

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

    <ListBox x:Name="Books"
             ItemsSource="{Binding Books}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Title}" />
                    <TextBlock Text="{Binding Author}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </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>

如果您的View的DataContext有您的集合,那么我不会太担心。那应该做的。

答案 1 :(得分:0)

由于@JackHughes,我得以修复它。 ViewModels必须是公共的,而不是内部的。将所有内容切换为公开即可解决。