A recent request came in asking for a sample demonstrating how to set the XamDataTree's ExpandedIconTemplate in code. The attached sample here demonstrates how you can create a FrameworkElementFactory object using an image and setting this as the DataTemplate's VisualTree property.
Hopefully, this can serve as a guide for other programmers wishing to do the same...
DataTemplate dt; public MainWindow() { InitializeComponent(); dt = new DataTemplate(); BitmapImage bi = new BitmapImage(new Uri(@"../../images/Sunset05.JPG", UriKind.Relative)); FrameworkElementFactory imageElementFactory = new FrameworkElementFactory(typeof(Image)); imageElementFactory.SetValue(Image.SourceProperty, bi); dt.VisualTree = imageElementFactory; dt.Seal(); this.xamDataTree1.ExpandedIconTemplate = dt; }
The result when running hte sample is that the image I specified is used for the expanded nodes' icon as below.
Hello Michael,
Thank you for your post. I have been looking into it and I modified the sample Francis has uploaded, so now it works as you want. Basically I put the active node in edit mode and after I applied the Template I put it back in display mode. This is needed because the Template should be updated once it is applied. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
I tried to add this to the ActiveNode event, but the image is not updated. Any ideas?
private void XamDataTree1_OnActiveNodeChanged(object sender, ActiveNodeChangedEventArgs e) { if (e.NewActiveTreeNode != null) { dt = new DataTemplate(); BitmapImage bi = new BitmapImage(new Uri(@"../../images/Sunset05.JPG", UriKind.Relative)); FrameworkElementFactory imageElementFactory = new FrameworkElementFactory(typeof (Image)); imageElementFactory.SetValue(Image.SourceProperty, bi); dt.VisualTree = imageElementFactory; dt.Seal(); //this.xamDataTree1.ExpandedIconTemplate = dt; e.NewActiveTreeNode.ExpandedIconTemplate = dt; e.NewActiveTreeNode.CollapsedIconTemplate = dt; } }