XAML noobie here! Here what i got now:
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; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.