0

次のモデルを使用して、TreeView にデータバインディングを作成したいと考えています。

public partial class MainWindow : Window
{
    private ViewModel model = new ViewModel();

    public MainWindow()
    {
        InitializeComponent();
        DataContext = model;
    }
    ...
}

public class ViewModel : ObservableObject
{
    private IList<Document> sourceDocuments;

    public IList<Document> SourceDocuments
    {
        get { return sourceDocuments; }
        set
        {
            sourceDocuments = value;
            OnPropertyChanged("SourceDocuments");
        }
    }

    public ViewModel()
    {
        SourceDocuments = new ObservableCollection<Document>();
    }
}

public class Document
{
    public String Filename;
    public IList<DocumentBlock> Blocks { get; set; }

    public Document(String filename)
    {
        this.Filename = filename;
        Blocks = new List<DocumentBlock>();
    }
}

public class DocumentBlock
{
    public String Name { get; private set; }

    public DocumentBlock(String name)
    {
        this.Name = name;
    }

    public override string ToString()
    {
        return Name;
    }
}

そして、このXAML

 <TreeView HorizontalAlignment="Left" Margin="6,6,0,6" Name="SourceDocumentsList" Width="202" 
     ItemsSource="{Binding SourceDocuments}">
     <TreeView.ItemTemplate>
         <HierarchicalDataTemplate ItemsSource="{Binding /Blocks}">
             <TextBlock Text="{Binding /Name}" />
         </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>
 </TreeView>

エラーメッセージが表示されます'Blocks' property not found on 'current item of collection' ''Document'。どうしてこれなの?

4

1 に答える 1