0

这是我的 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。请帮助我应该在 expander.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;
        }
    }
}

另外,请指点我一个资源,借助它我可以掌握这些棘手的绑定概念。请原谅我的长帖子(大部分是无用的,但为了清楚起见而发布)

这是我得到的输出:(输出缺少教师姓名

4

2 回答 2

1

乍一看,我看到你绑定到主题是这样的:

<ListBox ItemsSource="{Binding subjects}"

subjects您的模型中没有命名属性,并且Subjects是一个字段,因此它不起作用。您必须拥有一个具有精确(区分大小写)名称的属性,作为绑定才能工作。

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

于 2013-01-08T06:26:07.833 回答
0

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

于 2013-01-08T07:51:21.613 回答