简体   繁体   中英

Model's property not recognized by xaml (WPF MVVM)

Using mvvmCross, .net (5.0), visual studio 19

View model:

using System.ComponentModel;
using System.Threading.Tasks;
using MvvmCross.ViewModels;
using MvxR_M_S.Core.API;
using MvxR_M_S.Core.Models;

namespace MvxR_M_S.Core.ViewModels
{
    public class ArticlePresentationViewModel : MvxViewModel
    {
        public ArticlePresentationViewModel()
        {
        }

        public override async void ViewAppeared()
        {
            base.ViewAppeared();
            await LoadArticles(new ArticleEndpoint(new APIHelper()));
        }

        public async Task LoadArticles(ArticleEndpoint articleEndpoint)
        {
            var articleList = await articleEndpoint.GetAll();
            Articles = new BindingList<ArticleModel>(articleList);
        }

        private BindingList<ArticleModel> _articles;

        public BindingList<ArticleModel> Articles
        {
            get { return _articles; }
            set
            {
                _articles = value;
                SetProperty(ref _articles, value);
            }
        }
    }
}

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MvxR_M_S.Core.Models
{
    public class ArticleModel
    {
        public string ArticleName { get; set; }
    }
}

View:

<views:MvxWpfView
    xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
    xmlns:mvx="clr-namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf"
    x:Class="MvxR_M_S.Wpf.Views.ArticlePresentationView"
                 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:models="clr-namespace:MvxR_M_S.Core.Models;assembly=MvxR_M_S.Core"
                 xmlns:viewModels="clr-namespace:MvxR_M_S.Core.ViewModels;assembly=MvxR_M_S.Core"
                 mc:Ignorable="d" FontSize="20" Background="Wheat"
            d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="20" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center" Text="Article list" />

        <ListBox x:Name="Articles" ItemsSource="{Binding Articles}" Grid.Column="2" Grid.Row="2" MinHeight="300" MinWidth="300" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ArticleName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</views:MvxWpfView>

The problem is at the listbox, where it references ItemsSource right, also i did debug at the Articles property inside ViewModel, it holds right information, but nothing is presented on screen when started up.

So i guess this is binding issue.

Remove _articles = value; in Articles setter. SetProperty doesn't do anything if the field already is set

public BindingList<ArticleModel> Articles
    {
        get { return _articles; }
        set
        {
            SetProperty(ref _articles, value);
        }
    }

Since you are retrieving the articles in a separate thread, I think you have to do something like this:

Dispatcher.BeginInvoke(new Action(() =>  Articles = new BindingList<ArticleModel>(articleList);));

I am not exactly sure if it's the right way but Dispatcher.BeginInvoke seems needed from what I found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM