Lets take the example of a file system where you have something such as:
<Folder folderName="c:\">
<File fileName="somefile.txt" />
<Folder folderName="windows" />
</Folder>
I intentionally named them folderName and fileName to differentiate in the xaml example if this is possible. Some of the examples name almost everything "Title" or "Name" etc so it is hard to differentiate.
Is this possible? btw.. this would include using a specific file icon and folder icon. I know via code I can enumerate and add items, but im wondering if binding is possible via one xml datasource.
thanks!
Hi adamtuliper,
In order to achieve presenting this data in XamWebTree, you will have to read the XML and create an IEnumerable (e.g. ObservableCollection) of data objects, then bind the XamWebTree to this IEnumerable by setting the ItemsSource property and finally provide a Template for the automatically generated by the data binding XamWebTreeItems.
You will have to use the HierarchicalItemTemplate property of the XamWebTree and XamWebTreeItem and HierarchicalDataTemplate class in Infragistics.Silverlight.
See this post for more info.
In the data objects of your resulting from the XML IEnumerable, you will have to put either the "fileName" or "folderName" in a single property, e.g. "Name" and also, you will have to have an icon properties like e.g. ExpandedIcon and CollapsedIcon. For example:
public class MyDataFromXML { public string Name { get; set; } public BitmapImage CollapsedIcon { get; set; } public BitmapImage ExpandedIcon { get; set; } public ObservableCollection<MyDataFromXML> Children { get; set; } }
Your HierarchicalDataTemplate would be something like:
<ig:HierarchicalDataTemplate ItemsSource="{Binding Children}"> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> <ig:HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> </ig:HierarchicalDataTemplate.ItemTemplate> <ig:HierarchicalDataTemplate.DefaultItemsContainer> <DataTemplate> <igTree:XamWebTreeItem CollapsedIcon="{Binding CollapsedIcon}" ExpandedIcon="{Binding ExpandedIcon}"/> </DataTemplate> </ig:HierarchicalDataTemplate.DefaultItemsContainer> </ig:HierarchicalDataTemplate>
Please, let us know if you have more questions.
HTH,
thanks for the reply. It is still a bit off.. using that template with this data:
<?xml version="1.0" encoding="utf-8" ?><fileSystem> <folder name="c:\"> <file name="file1.txt" /> <file name="file2.txt" /> <folder name="windows"> <file name="sub item 1" /> <file name="sub item 2" /> <file name="sub item 3" /> <file name="sub item 4" /> </folder> </folder>
<folder name="d:\"> <file name="file10.txt" /> <file name="file11.txt" /> <file name="file12.txt" /> <folder name="program files"> <file name="program1" /> <file name="program2" /> </folder> </folder></fileSystem>
I receive a tree that looks like: (note items with * are incorrect)
c:\ file1.txt file2.txt sub item 1 (*) sub item 2 (*) sub item 3 (*) sub item 4 (*)windows sub items 1-4d:\ file10.txt file11.txt file12.txt program1 (*) program2 (*)
program files program1 program2
so it folds up the subfolders into the current folder IE doesnt show the folder windows under c:\
Hi,
Could you post the code which is for your data object class and the collection which you use as a data source?
Also, the code which assigns the data source to the tree and the template you apply?
Thanks,
I've tried two ways of doing this. Im sure its something simple. The sample you sent above I couldnt use exactly as I received an error upon binding.
<igTree:XamWebTree x:Name="dataTree" Background="Transparent" BorderThickness="1,1,1,1" HorizontalAlignment="Stretch" IsCheckBoxThreeState="True" CheckBoxVisibility="Visible"> <igTree:XamWebTree.HierarchicalItemTemplate> <ig:HierarchicalDataTemplate ItemsSource="{Binding Children}"> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> <ig:HierarchicalDataTemplate.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> </ig:HierarchicalDataTemplate.ItemTemplate> <ig:HierarchicalDataTemplate.DefaultItemsContainer> <DataTemplate> <igTree:XamWebTreeItem CollapsedIcon="{Binding CollapsedIcon}" ExpandedIcon="{Binding ExpandedIcon}"/> </DataTemplate> </ig:HierarchicalDataTemplate.DefaultItemsContainer> </ig:HierarchicalDataTemplate>
</igTree:XamWebTree.HierarchicalItemTemplate> </igTree:XamWebTree>
public class MyDataFromXML { public string Name { get; set; } public BitmapImage CollapsedIcon { get; set; } public BitmapImage ExpandedIcon { get; set; } public IEnumerable<MyDataFromXML> Children { get; set; } }
for the binding code I tried this way:
List<MyDataFromXML> folderItems = new List<MyDataFromXML>(); folderItems.Add(new MyDataFromXML() { Name = "somefile.txt" });
MyDataFromXML folder1 = new MyDataFromXML(); folder1.Name = "folder1";
MyDataFromXML subFolder = new MyDataFromXML(); subFolder.Name = "subfolder"; List<MyDataFromXML> subFolderItems = new List<MyDataFromXML>() { new MyDataFromXML() { Name = "subfolderfile.txt" } };
folder1.Children = new List<MyDataFromXML>() { subFolder }; subFolder.Children = subFolderItems; folderItems.Add(subFolder);
//format should be: //folder1 // somefile.txt // subfolder // subfolderfile.txt
this.dataTree.ItemsSource = new List<MyDataFromXML> { folder1 }; this.dataTree.XamWebTreeItems[0].IsExpanded = true;
as well as this way:
XDocument doc = XDocument.Load(xmlFile); var dataSource = (from d in doc.Descendants("folder") select new MyDataFromXML { Name = d.Attribute("name") == null ? "" : d.Attribute("name").Value, Children = this.GetItems(d) });
this.dataTree.ItemsSource = dataSource.ToList(); this.dataTree.XamWebTreeItems[0].IsExpanded = true;
//This is using the same xml as posted above although I changed all 'title' to 'name'
//I've tried variations on this getting folder/file elements. I may just be visualizing this incorrectly based on required layout.
private IEnumerable<MyDataFromXML> GetItems(XElement parent) {
var items = (from d in parent.Descendants() select new MyDataFromXML { Name = d.Attribute("name") == null ? "" : d.Attribute("name").Value, Children = this.GetItems(d) }); return items; }
What if I want to define the Template of the child nodes?
I mean, I need to define the node of 2 levels withot modify the dataTemplate, I add the child nodes programmatically... and that work fine but I need to add other icon to those new nodes in the second level...
I use this code to define the first level template but I cant modify the expandedicontemplate for the child nodes...
<code>
<
UserControl.Resources>
<DataTemplate x:Key="DriveCollapsedIcon">
<Grid>
<Image Source="/Via.SaaS.FrontEnd;Component/Images/Drive.png" Height="17" Width="17" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="DirectoryExpandedIcon">
<Image Source="/Via.SaaS.FrontEnd;Component/Images/OpenFolder.png" Height="20" Width="20" />
<DataTemplate x:Key="DirectoryCollapsedIcon">
<Image Source="/Via.SaaS.FrontEnd;Component/Images/CloseFolder.png" Height="15" Width="15" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<igTree:XamWebTree x:Name="test" HorizontalAlignment="Left" Margin="21,22,0,56" Width="222" ExpandedIconTemplate="{StaticResource DirectoryExpandedIcon}" CollapsedIconTemplate="{StaticResource DirectoryCollapsedIcon
}" >
</igTree:XamWebTree>
</code>