I have been asked to update a TreeVeiw in a WPF application that is built on the fly from a class object. As you will see treeveiw is not bound to anything. Below is not my code!!
<TreeView Grid.Row="0" HorizontalAlignment="Stretch" Name="tvLocations" VerticalAlignment="Stretch" SelectedItemChanged="tvLocations_SelectedItemChanged" />
private void BuildTreeVeiw(Location locationList)
{
this.Title = _selectedLoc.Name + " - Locations";
tvLocations.Items.Clear();
TreeViewItem tvitem;
tvitem = new TreeViewItem() { Header = locationList.Name, Uid = locationList.Id.ToString() };
if (locationList.Printers != null)
{
TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
tvprnitem.FontWeight = FontWeights.Regular;
foreach (Printer sprinters in locationList.Printers)
{
TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name, Uid = sprinters.Id.ToString() };
TreeViewItem psubitem1 = new TreeViewItem() { Header = String.Concat("UNC: ", sprinters.UNC) };
psubitem.Items.Add(psubitem1);
tvprnitem.Items.Add(psubitem);
}
tvitem.Items.Add(tvprnitem);
}
foreach (Location loc in locationList.Children)
{
AddChildren(loc, ref tvitem);
}
tvLocations.Items.Add(tvitem);
}
private void AddChildren(Location child, ref TreeViewItem tvi)
{
TreeViewItem tvitem;
tvitem = new TreeViewItem() { Header = child.Name, Uid = child.Id.ToString() };
if (child.Name == _currentLocation.Name)
{
tvitem.FontWeight = FontWeights.Bold;
}
if (child.Printers != null)
{
TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
tvprnitem.FontWeight = FontWeights.Regular;
foreach (Printer sprinters in child.Printers)
{
TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name, Uid = sprinters.Id.ToString() };
TreeViewItem psubitem1 = new TreeViewItem() { Header = String.Concat("UNC: ", sprinters.UNC) };
psubitem.Items.Add(psubitem1);
tvprnitem.Items.Add(psubitem);
}
tvitem.Items.Add(tvprnitem);
}
if (child.Children != null)
{
foreach (Location loc in child.Children)
{
AddChildren(loc, ref tvitem);
}
}
tvi.Items.Add(tvitem);
}
This builds the tree correctly and all I have been asked to do is add an icon to the TreeViewItem. The icon will be a different depending on whether it is a location or a printer within that location.
I cannot see how to add icons to TreeViewItems can anyone point me in the right direction?
I solved this by changing this line
tvitem = new TreeViewItem() { Header = child.Name, Uid = child.Id.ToString() };
To
tvitem = GetTreeView(child.Id.ToString(), child.Name, "location.png");
Adding this function
private TreeViewItem GetTreeView(string uid, string text, string imagePath)
{
TreeViewItem item = new TreeViewItem();
item.Uid = uid;
item.IsExpanded = false;
// create stack panel
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
// create Image
Image image = new Image();
image.Source = new BitmapImage
(new Uri("pack://application:,,/Images/" + imagePath));
image.Width = 16;
image.Height = 16;
// Label
Label lbl = new Label();
lbl.Content = text;
// Add into stack
stack.Children.Add(image);
stack.Children.Add(lbl);
// assign stack to header
item.Header = stack;
return item;
}
Worked perfectly.
Very nice. I just add this inside method wich return stack panel, to make code much readable:
private StackPanel CustomizeTreeViewItem(object itemObj)
{
// Add Icon
// Create Stack Panel
StackPanel stkPanel = new StackPanel();
stkPanel.Orientation = Orientation.Horizontal;
// Create Image
Image img = new Image();
img.Source = new BitmapImage(new Uri("pack://application:,,/Resources/control.png"));
img.Width = 16;
img.Height = 16;
// Create TextBlock
TextBlock lbl = new TextBlock();
lbl.Text = itemObj.ToString();
// Add to stack
stkPanel.Children.Add(img);
stkPanel.Children.Add(lbl);
return stkPanel;
}
and in treeView Initialization
// Assign stack to header
item.Header = CustomizeTreeViewItem(itemObj);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With