【问题标题】:ListView isn't populated with list after setting up binding设置绑定后 ListView 未填充列表
【发布时间】:2015-05-03 21:04:10
【问题描述】:

我在此应用程序的主页中设置了两个列表视图。但是使用来自 VM 的 gradesubject 属性的绑定无法按预期工作以显示数据列表。

MVVM 灯光库用于帮助在应用程序中遵循 MVVM 模式,因此如果需要使用该库以不同方式设置绑定。

我尝试通过检查以下内容进行调试,但无济于事:

  • 检查数据上下文是否设置
  • 检查属性名称是否正确且绑定语法正确

有谁知道为什么在运行时列表数据没有填充到 ListView 中?

模型设置如下,包含两个列表,我需要绑定到视图中的列表视图:

namespace LC_Points.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            //call methods to initilise list data
            GetGradeTypes();
            GetSubjectTypes();
        }


        private List<Grade> grades { get; set; }
        private List<Grade> subjects { get; set; }


        public void GetGradeTypes()
        {
            List<Grade> gradeList = new List<Grade>();

            // Adding Grades to List
            gradeList.Add(new Grade { grade = "A1" });
            gradeList.Add(new Grade { grade = "A2" });
            gradeList.Add(new Grade { grade = "B1" });
            gradeList.Add(new Grade { grade = "B2" });
            gradeList.Add(new Grade { grade = "B3" });
            gradeList.Add(new Grade { grade = "C1" });
            gradeList.Add(new Grade { grade = "C2" });
            gradeList.Add(new Grade { grade = "C3" });
            gradeList.Add(new Grade { grade = "D1" });
            gradeList.Add(new Grade { grade = "D2" });
            gradeList.Add(new Grade { grade = "D3" });
            gradeList.Add(new Grade { grade = "E,F,NG" });

            grades = gradeList;

        }


        public void GetSubjectTypes()
        {
            List<Grade> subjectList = new List<Grade>();

            // Adding Subjects to List
            subjectList.Add(new Grade { subject = "Accounting" });
            subjectList.Add(new Grade { subject = "Agricultural Economics" });
            subjectList.Add(new Grade { subject = "Agricultural Science" });
            subjectList.Add(new Grade { subject = "Ancient Greek" });
            subjectList.Add(new Grade { subject = "Applied Math" });
            subjectList.Add(new Grade { subject = "Biology" });
            subjectList.Add(new Grade { subject = "Business" });
            subjectList.Add(new Grade { subject = "Business Group" });
            subjectList.Add(new Grade { subject = "Chemistry" });
            subjectList.Add(new Grade { subject = "Classical Studies" });
            subjectList.Add(new Grade { subject = "Engineering" });
            subjectList.Add(new Grade { subject = "English" });

            subjects = subjectList;

        }

    }
}

静态资源Locator在App.xaml中定义如下:

<Application.Resources>
        <vm:ViewModelLocator xmlns:vm="using:LC_Points.ViewModel" x:Key="Locator" />
    </Application.Resources>

这是具有两个列表视图的视图:

<Page x:Class="LC_Points.MainPage"
      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="using:LC_Points"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
      DataContext="{Binding Source={StaticResource Locator},
                            Path=MainViewModel}"
      mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="90*" />
            <RowDefinition Height="10*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <ListView x:Name="subjectOneLbx"
                  Grid.ColumnSpan="2"
                  Width="211"
                  Height="48"
                  Margin="10,159,0,368.833"
                  HorizontalAlignment="Left"
                  ItemsSource="{Binding subjects}" />

        <ListView x:Name="gradeOneLbx"
                  Grid.Column="1"
                  Grid.ColumnSpan="2"
                  Width="49"
                  Height="48"
                  Margin="132.667,159,0,368.833"
                  HorizontalAlignment="Left"
                  ItemsSource="{Binding grades}" />
    </Grid>
</Page>

这是应用程序的数据模型:

namespace LC_Points.Model
{
    public class Grade : INotifyPropertyChanged
    {

        // The name of the subject.
        public string subject { get; set; }

        // The type of Grade, eg, A, B2 etc..
        public string grade { get; set; }

        // The points paired with each grade type.
        public int points { get; set; }


        private int _count;
        public int Count
        {
            get
            {
                return _count;
            }
            set
            {
                _count = value;
                RaisePropertyChanged("Count");
            }
        }





        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


    }
}

项目源码树结构如下:

【问题讨论】:

  • 你是如何定义静态资源“Locator”的?
  • 刚刚将其添加到我上面的问题 ^^ 中,我将其添加到了我的 app.xaml。

标签: c# list listview windows-phone-8.1 mvvm-light


【解决方案1】:

您需要将机器人 gradessubjects 设为 public 属性。

【讨论】:

  • 好吧,没发现,我已经将gradessubjects 这两个属性的访问修饰符更新为公开。但是当我在设备上运行应用程序时,列表仍然不会显示在我的视图中。任何其他想法为什么会这样?如果这对错误有任何影响,我也在上面发布了我的模型。
  • 我还添加了我在 app.xaml 中定义定位器的方式以及我的源代码树的屏幕截图以供参考。
【解决方案2】:

您已将视图绑定到模型的私有属性。数据绑定仅适用于公共属性。

MSDN:

您用作绑定源属性的属性必须 成为您班级的公共财产。明确定义的接口 不能出于绑定目的访问属性,也不能受保护, 没有基础的私有、内部或虚拟属性 实施。

【讨论】:

    【解决方案3】:

    我之前没有使用过 mvvm-light 类,所以我不能对此发表评论。但是,请考虑对 App.xaml 的这些更改。


    案例 1:将 App.xaml 更改为此并更改 MainViewModel 中的命名空间:

    <Application
        x:Class="LC_Points.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:LC_Points">
    
        <Application.Resources>
            <local:MainViewModel x:Key="Locator"/>
        </Application.Resources>
    
    </Application>
    

    MainViewModel.cs:

    namespace LC_Points
    {
        /// <summary>
        /// ...
        /// </summary>
        public class MainViewModel
        {
            /// <summary>
            /// ...
            /// Initializes a new instance of the MainViewModel class.
            /// </summary>
            public MainViewModel()
    

    案例 2:保持 MainViewModel 不变,并更改 App.xaml

    <Application
        x:Class="LC_Points.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:LC_Points.ViewModel">
    
        <Application.Resources>
            <local:MainViewModel x:Key="Locator"/>
        </Application.Resources>
    
    </Application>
    

    使用您拥有的原始 xaml,内容几乎不会显示,并且由于您没有提供数据模板,您将获得 LC_Points.Model.Grade 的成绩和科目中的每个条目。所以,我把它改成了这个(可能不是你想要的,只是为了说明):

    <Page
        x:Class="LC_Points.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:LC_Points"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
        DataContext="{Binding Source={StaticResource Locator}}">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="90*" />
                <RowDefinition Height="10*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
    
            <ListView x:Name="subjectOneLbx" Grid.RowSpan="2" Margin="10" HorizontalAlignment="Left"
                      ItemsSource="{Binding subjects}" SelectionMode="None">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding subject}" Margin="5"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    
            <ListView x:Name="gradeOneLbx" Grid.Column="1" Margin="10" HorizontalAlignment="Left"
                      ItemsSource="{Binding grades}" SelectionMode="None">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding grade}" Margin="5"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Page>
    

    我得到了以下结果

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多