ここに私の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 に何を書くべきか教えてください。学生は Expander のコンテンツとして関連付けられています。機能しない行を書きました。
<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;
}
}
}
また、これらのトリッキーなバインディングの概念を習得できるリソースを教えてください。長い投稿をお許しください(ほとんどは役に立たないものですが、わかりやすくするために投稿しています)
これが私が得ている出力です:(