How can I change the TreeView Icon into a folder icon?
Posted
by
KDP
on Stack Overflow
See other posts from Stack Overflow
or by KDP
Published on 2011-03-22T13:27:17Z
Indexed on
2012/04/12
23:29 UTC
Read the original article
Hit count: 191
I'm trying to change the icon of my TreeView in a folder icon. Also when it collapses it needs to have an opened folder icon.
My treeview has databound items in it and the code is:
<TreeView x:Name="TreeViewCategories" Grid.Row="0" Grid.Column="1" Height="610" HorizontalAlignment="Left" Margin="29,111,0,0" VerticalAlignment="Top" Width="315" BorderThickness="0" Background="Transparent" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<TextBlock FontSize="20" Text="{Binding Name}" PreviewMouseDown="TextBlock_PreviewMouseDown"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Also this is how I fill the treeview with items from XML (It's a snipped out of alot of code:
private void LoadHospitalXML()
{
try
{
FileStream fs = new FileStream("ConfigOrgHospital.xml", FileMode.Open, FileAccess.Read);
var xml = XmlReader.Create(fs);
rootElement = ConvertHospitalData(xml);
this.TreeViewCategories.ItemsSource = null;
List<HospitalWrapper> li = new List<HospitalWrapper>();
var hosp = rootElement.Items.FirstOrDefault();
if (hosp != null)
{
foreach (var i in hosp.Hospital)
{
li.AddIfNotNull(CreateHospList(i));
}
}
this.TreeViewCategories.ItemsSource = li;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private HospitalWrapper CreateHospList(object obj)
{
var newItem = new HospitalWrapper();
newItem.Context = obj;
//Hospital Names//
if (obj is HospitalDataHospitalsHospital)
{
var hosp = (HospitalDataHospitalsHospital)obj;
//newItem.Title = "Hospitals";
newItem.Name = hosp.DefaultName;
var tmp = new HospitalWrapper();
tmp.Name = "Sites";
tmp.IsTitle = true;
if (hosp.Sites != null)
foreach (var i in hosp.Sites)
{
tmp.Items.AddIfNotNull(CreateHospList(i));
}
newItem.Items.Add(tmp);
tmp = new HospitalWrapper();
tmp.Name = "Specialties";
tmp.IsTitle = true;
if (hosp.Deps != null)
foreach (var j in hosp.Deps)
{
tmp.Items.AddIfNotNull(CreateHospList(j));
}
newItem.Items.Add(tmp);
}
}
© Stack Overflow or respective owner