为叶子节点添加图片的代码如下,主要思路是指定C1TreeViewItem.Header属性
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- InitializeTreeView();
- }
- void InitializeTreeView()
- {
- // Remove items that were added at design time
- c1TreeView1.Items.Clear();
- c1TreeView1.ShowLines = false;
- C1TreeViewItem booklist = new C1TreeViewItem();
- booklist.Header = "Book List";
- c1TreeView1.Items.Add(booklist);
- // Adding child items
- C1TreeViewItem language = new C1TreeViewItem();
- language.Header = "Language Books";
- booklist.Items.Add(language);
- // Adding child items
- C1TreeViewItem security = new C1TreeViewItem();
- security.Header = "Security Books";
- booklist.Items.Add(security);
- // Adding child items
- C1TreeViewItem classic = new C1TreeViewItem();
- classic.Header = "Classic Books";
- booklist.Items.Add(classic);
- // Adding child items
- C1TreeViewItem subclassic = new C1TreeViewItem();
- StackPanel sp1 = new StackPanel();
- sp1.Orientation = Orientation.Horizontal;
- sp1.Children.Add(new Image() { Width = 14, Height = 14, Source = new BitmapImage(new Uri("Resources/Book.png",UriKind.Relative)) });
- sp1.Children.Add(new TextBlock() { FontSize = 11, Margin = new Thickness(4, 0, 0, 0), Text = "Catch-22" });
- subclassic.Header = sp1;
- classic.Items.Add(subclassic);
- C1TreeViewItem subclassic2 = new C1TreeViewItem();
- StackPanel sp2 = new StackPanel();
- sp2.Orientation = Orientation.Horizontal;
- sp2.Children.Add(new Image() { Width = 14, Height = 14, Source = new BitmapImage(new Uri("Resources/Book.png", UriKind.Relative)) });
- sp2.Children.Add(new TextBlock() { FontSize = 11, Margin = new Thickness(4, 0, 0, 0), Text = "The Great Gatsby" });
- subclassic2.Header = sp2;
- classic.Items.Add(subclassic2);
- }
- }
复制代码
|