c# - xaml无法识别模型的属性(WPF MVVM)

标签 c# wpf xaml mvvm mvvmcross

使用mvvmCross,.net(5.0),Visual Studio 19
查看模型:

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);
            }
        }
    }
}
模型:
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; }
    }
}
看法:
<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>
问题出在列表框上,它引用了正确的 ItemsSource ,我也确实在ViewModel的 Articles 属性中进行了调试,它保留了正确的信息,但是启动时屏幕上没有任何显示。
所以我想这是有约束力的问题。

最佳答案

在文章 setter 中删除_articles = value;。如果已经设置了字段,SetProperty将不执行任何操作

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

关于c# - xaml无法识别模型的属性(WPF MVVM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65873015/

相关文章:

c# - 创建富文本模板

c# - 如何向数据绑定(bind)的 WPF 组合框添加分隔符?

c# - WPF ListBox 多选绑定(bind)

c# - 避免在处理控件时调用 Invoke

c# - 表格开始最小化

c# - 如果我有一个 TextBox 是 Checkbox 的子项,是否可以输入破折号?

c# - 如何将现代 UI 菜单文本更改为大写

wpf - 带有水平项目的 ListView

c# - 如何在 WPF 中动态填充 treeview

C# 堆栈溢出覆盖 EIP