这是我的 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;
}
}
}
另外,请指点我一个资源,借助它我可以掌握这些棘手的绑定概念。请原谅我的长帖子(大部分是无用的,但为了清楚起见而发布)
这是我得到的输出:(