XAML:
<Window x:Class="TreeViewWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView Name="treeView">
</TreeView>
</Grid>
</Window>
Код позади:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CreateTree();
}
private void CreateTree()
{
treeView.Items.Add(GetTreeView("text"));
}
private TreeViewItem GetTreeView(string text)
{
TreeViewItem newTreeViewItem = new TreeViewItem();
// create stack panel
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
// create Image
Image image = new Image();
image.Source = new BitmapImage(new Uri(@"/Images/YourImage.png", UriKind.Relative));
// Label
TextBlock lbl = new TextBlock();
lbl.Text = text;
lbl.TextWrapping = TextWrapping.Wrap;
lbl.Width = 139;
// Add into stack
stack.Children.Add(image);
stack.Children.Add(lbl);
// assign stack to header
newTreeViewItem.Header = stack;
return newTreeViewItem;
}
}
}
Обратите внимание, что image.Source = new BitmapImage(new Uri(@"/Images/YourImage.png", UriKind.Relative))
где "/ Images /" - имя папки.
Это приложение MVVM? или код за окном? – StepUp
Я имею в виду C# -код за приложением wpf. – MyNameIsHans
Является ли изображение неизменным? Или это зависит от дерева? – Jens