Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
5124
Setting ExpandedIconTemplate in Code (a simple example)
posted

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.

XDT_DataTemplateCode_WPF.zip