How to add TreeViewItem dynamically in TreeView control in WPF. Here is the coding example given below to add TreeViewItem in TreeView control.
public TreeViewItem CreateTreeViewItem(string nodeName, string headerText, string frontImagePath, string tailImagePath, bool isCheckBoxRequired)
{
TreeViewItem treeViewItem = new TreeViewItem();
try
{
StackPanel stackPanel = new StackPanel();
Label lblHeaderText = new Label();
Image imgFrontIcon, imgTailIcon;
imgFrontIcon = new Image(); imgTailIcon = new Image();
stackPanel.Orientation = Orientation.Horizontal;
if (isCheckBoxRequired)
{
CheckBox chkbox = new CheckBox();
chkbox.Name = IDPrefixForControls.Chk_.ToString() + nodeName;
chkbox.HorizontalAlignment = HorizontalAlignment.Center;
chkbox.VerticalAlignment = VerticalAlignment.Center;
stackPanel.Children.Add(chkbox);
}
if (frontImagePath != null && frontImagePath != string.Empty)
{
Uri uri = new Uri(ConstantVariables.UriString + frontImagePath);
BitmapImage bitMapSource = new BitmapImage();
bitMapSource.BeginInit();
bitMapSource.UriSource = uri;
bitMapSource.EndInit();
imgFrontIcon.Source = bitMapSource;
}
if (tailImagePath != null && tailImagePath != string.Empty)
{
Uri uri = new Uri(ConstantVariables.UriString + tailImagePath);
BitmapImage bitMapSource = new BitmapImage();
bitMapSource.BeginInit();
bitMapSource.UriSource = uri;
bitMapSource.EndInit();
imgTailIcon.Source = bitMapSource;
}
lblHeaderText.Content = headerText;
stackPanel.Children.Add(imgFrontIcon);
stackPanel.Children.Add(lblHeaderText);
stackPanel.Children.Add(imgTailIcon);
nodeName = nodeName.Replace("-", "_").Replace(" ", "_");
treeViewItem.Name = nodeName;
treeViewItem.Header = stackPanel;
}
catch (Exception ex)
{ LogExceptionDetails(ex); }
return treeViewItem;
}
public TreeViewItem CreateTreeViewItem(string nodeName, string headerText, string frontImagePath, string tailImagePath, bool isCheckBoxRequired)
{
TreeViewItem treeViewItem = new TreeViewItem();
try
{
StackPanel stackPanel = new StackPanel();
Label lblHeaderText = new Label();
Image imgFrontIcon, imgTailIcon;
imgFrontIcon = new Image(); imgTailIcon = new Image();
stackPanel.Orientation = Orientation.Horizontal;
if (isCheckBoxRequired)
{
CheckBox chkbox = new CheckBox();
chkbox.Name = IDPrefixForControls.Chk_.ToString() + nodeName;
chkbox.HorizontalAlignment = HorizontalAlignment.Center;
chkbox.VerticalAlignment = VerticalAlignment.Center;
stackPanel.Children.Add(chkbox);
}
if (frontImagePath != null && frontImagePath != string.Empty)
{
Uri uri = new Uri(ConstantVariables.UriString + frontImagePath);
BitmapImage bitMapSource = new BitmapImage();
bitMapSource.BeginInit();
bitMapSource.UriSource = uri;
bitMapSource.EndInit();
imgFrontIcon.Source = bitMapSource;
}
if (tailImagePath != null && tailImagePath != string.Empty)
{
Uri uri = new Uri(ConstantVariables.UriString + tailImagePath);
BitmapImage bitMapSource = new BitmapImage();
bitMapSource.BeginInit();
bitMapSource.UriSource = uri;
bitMapSource.EndInit();
imgTailIcon.Source = bitMapSource;
}
lblHeaderText.Content = headerText;
stackPanel.Children.Add(imgFrontIcon);
stackPanel.Children.Add(lblHeaderText);
stackPanel.Children.Add(imgTailIcon);
nodeName = nodeName.Replace("-", "_").Replace(" ", "_");
treeViewItem.Name = nodeName;
treeViewItem.Header = stackPanel;
}
catch (Exception ex)
{ LogExceptionDetails(ex); }
return treeViewItem;
}