无法将列表绑定为扩展器的内容

时间:2013-01-08 06:19:11

标签: c# .net silverlight xaml binding

这是我的Subject.cs

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ListBoxBinding.Models
{
    public class Subject
    {
        private string name;
        public String Name
        {
            get{
                return name;
            }
            set{
                name=value;
            }
        }
        private string faculty;
        public string Faculty
        {
            get{
                return faculty;
            }
            set{
                faculty=value;
            }
        }
        private int hours;
        public int Hours
        {
            get
            {
                return hours;
            }
            set
            {
                hours = value;
            }
        }
    }
}

这是我的Student.cs

using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ListBoxBinding.Models
{
    public class Student
    {
        private string name;
        public String Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public ObservableCollection<Subject> Subjects = new ObservableCollection<Subject>();
    }
}

这是我的MyList.xaml。请帮我看看我应该在expandder.content中写什么,以便我可以得到主题的教师名单,学生与扩展器的联系人相关联。我写了一行不起作用。

<UserControl
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="ListBoxBinding.Views.MyList"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <ItemsControl x:Name="MyItemsControl">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <toolkit:Expander HorizontalAlignment="Left" Height="100" Width="150" Header="{Binding Name}">
                        <toolkit:Expander.Content>
                            <!-- Help needed here -- what should i write here? -->
                            <ListBox ItemsSource="{Binding subjects}" DisplayMemberPath="Faculty"/>
                        </toolkit:Expander.Content>
                    </toolkit:Expander>
                </DataTemplate>
            </ItemsControl.ItemTemplate>

        </ItemsControl>


    </Grid>
</UserControl>

这是我的MyList.xaml.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ListBoxBinding.Models;
namespace ListBoxBinding.Views
{
    public partial class MyList : UserControl
    {
        public ObservableCollection<Student> students = new ObservableCollection<Student>();

        public void SetData()
        {
            for(int i=1;i<=5;i++)
            {
                Student s = new Student();
                s.Name="Student "+i.ToString();
                students.Add(s);
            }
            foreach(Student s in students)
            {
                for(int i=1;i<=5;i++)
                {
                    Subject sj = new Subject();
                    sj.Name="subject "+i;
                    sj.Faculty = "faculty"+i;
                    sj.Hours = i+10;
                    s.Subjects.Add(sj);
                }
            }
        }
        public MyList()
        {
            InitializeComponent();
            SetData();
            MyItemsControl.ItemsSource = students;
        }
    }
}

另外,请指出一个资源,借此我可以掌握这些棘手的绑定概念。请原谅我的长篇文章(大部分都没用,但为了清晰起见而张贴)

以下是我得到的输出:(The output is missing faculty names

2 个答案:

答案 0 :(得分:1)

乍一看,我发现你和受试者的关系如下:

<ListBox ItemsSource="{Binding subjects}"

模型中没有名为subjects的属性,而Subjects是一个字段,因此无效。您必须拥有一个具有精确(区分大小写)名称的属性,以及绑定它才能工作。

至于您未来的进展,请查看INotifyPropertyChanged界面及其在绑定中的作用。

答案 1 :(得分:0)

在您的示例中,绑定路径是错误的。首先,您应将ItemsControl源设置为Students。然后,对于内部ListBox,您应该为Subjects设置其ItemsSource。请确保您的集合是公共属性,并且所有有界对象都应实现INotifyPropertyChanged接口以进行进一步的UI刷新。