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
Parents
No Data
Reply
  • 190
    posted

    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;
                }
            }

Children