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 に何を書くべきか教えてください。学生は 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;
        }
    }
}

また、これらのトリッキーなバインディングの概念を習得できるリソースを教えてください。長い投稿をお許しください(ほとんどは役に立たないものですが、わかりやすくするために投稿しています)

これが私が得ている出力です:(出力に教員名がありません

4

2 に答える 2

1

一見すると、あなたは次のように主題に拘束されていることがわかります。

<ListBox ItemsSource="{Binding subjects}"

subjectsモデルに名前が付けられたプロパティはなくSubjects、フィールドであるため、機能しません。それが機能するためには、バインディングのように正確な(大文字と小文字を区別する)名前のプロパティが必要です。

今後の進捗状況については、INotifyPropertyChangedインターフェイスとバインディングにおけるその役割を確認してください。

于 2013-01-08T06:26:07.833 に答える
0

あなたの例では、バインディング パスが間違っています。まず、ItemsControl ソースを Student として設定する必要があります。次に、内側の ListBox に対して、サブジェクトの ItemsSource を設定する必要があります。コレクションがパブリック プロパティであることを確認してください。さらに UI を更新するには、すべてのバインドされたオブジェクトに INotifyPropertyChanged インターフェイスを実装する必要があります。

于 2013-01-08T07:51:21.613 に答える