سؤال

XAML noobie here! Here what i got now:

  • MachineList, is a List of Machine objects (a custom class i made)
  • Every Machine has a List of drives inside of it (drive is another class)

Now i need to list those and let the user check some machines or drives, displaying some drive information. My first goal is to bind my Machine List to a Treeview (for root elements) and Drives for level 1 children, but i really can't do it!

Here what i'm doing:

<TreeView ItemsSource="{Binding MachineList}" Name="treeView1">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding DriveList}">
            <TextBlock Text="{Binding driveName}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

Here how i declared MachineList in C# code:

public ObservableCollection<MyContainerClass.Machine> MachineList{ get; set; }

as i can verify during debug my MachineList contains the correct data (even the DriveList is correctly filled), but nothing is displayed on my Treeview. DriveList is a ObservableCollection<MyContainerClass.Drive> object.

What am i doing wrong?

هل كانت مفيدة؟

المحلول

Set the ItemTemplate for your HierarchicalDataTemplate as follows.

<TreeView ItemsSource="{Binding MachineList}" Name="treeView1">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding DriveList}">
                    <TextBlock Text="{Binding machineName}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding driveName}" />
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

Update(the code i have tried that works for me):

public class Machines
    {
        private string machinename;

        public string machineName
        {
            get { return machinename; }
            set { machinename = value; }
        }
        private ObservableCollection<DriveList> driveList;

        public ObservableCollection<DriveList> DriveList
        {
            get { return driveList; }
            set { driveList = value; }
        }


    }

    public class ViewModel
    {
        private ObservableCollection<Machines> machines;

        public ObservableCollection<Machines> MachineList
        {
            get { return machines; }
            set { machines = value; }
        }
        public ViewModel()
        {
            ObservableCollection<DriveList> list= new ObservableCollection<DriveList>();
            list.Add(new DriveList() { driveName = "Drive 1" });
            machines = new ObservableCollection<Machines>();
            machines.Add(new Machines() { machineName = "Machine 1", DriveList =list  });
        }
    }

    public class DriveList
    {
        private string drivename;

        public string driveName
        {
            get { return drivename; }
            set { drivename = value; }
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top