2

I have been trying to make a treeview that looks something like

2001(root)
-Student1(node)
-Student2(node)

I've tried to use hierarchicaldatatemplates but I'm still not grasping what I need to. This is my code that i'm looking to bind my treeview to. Any help with the Xaml would be appriciated.

I thought it would look something like

    <TreeView ItemsSource="{Binding CurrentClass}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Student}" ItemsSource="{Binding CurrentClass.Students}">
                    <TextBlock Text="{Binding CurrentClass.Students/FirstName}" />
                </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
public class ViewModel
{
    public FreshmenClass currentClass = new FreshmenClass();

    public ViewModel()
    {
        currentClass.Year = "2001";
        currentClass.Students.Add(new Student("Student1", "LastName1"));
        currentClass.Students.Add(new Student("Student2", "LastName2"));
    }

    public FreshmenClass CurrentClass
    {
        get { return currentClass; }
    }
}

public class FreshmenClass
{
    public string Year { get; set; }
    public List<Student> students = new List<Student>();

    public List<Student> Students
    {
        get { return students; }
        set { students = value; }
    }
}

public class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Student(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

1 Answer 1

5

Take a look to the documentation about Treeview and HierarchicalDataTemplate. Anyway I edit your example like this (XAML):

<TreeView ItemsSource="{Binding CurrentClass}" >
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Students}">
                <TextBlock Text="{Binding Year}" />
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding FirstName}"> </TextBlock>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

and c#:

public class ViewModel 
{
    private List<FreshmenClass> currentClass;

    public ViewModel()
    {
        CurrentClass = new List<FreshmenClass>();
        FreshmenClass temp = new FreshmenClass();
        temp.Year = "2001";
        temp.Students.Add(new Student("Student1", "LastName1"));
        temp.Students.Add(new Student("Student2", "LastName2"));

        CurrentClass.Add(temp);
    }

    public List<FreshmenClass> CurrentClass
    {
        get { return currentClass; }
        set { currentClass = value; }
    }
}

the ItemsSource property is an IEnumerable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.