Microsoft Developer Network > Forenhomepage > Windows Presentation Foundation (WPF) > HierarchicalDataTemplate for TreeView programmatically (C#).
Stellen Sie eine FrageStellen Sie eine Frage
 

BeantwortetHierarchicalDataTemplate for TreeView programmatically (C#).

  • Donnerstag, 2. Juli 2009 18:52kicaj TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code
    I have this class:
        public class Folder
        {
            private string _name;
            internal string Name
            {get { return _name; }}
    
            private List<Folder> _subfolders;
            internal List<Folder> Subfolders
            { get { return _subfolders; } }
    
            public Folder()
            {
                _name = "Root";
                _subfolders = new List<Folder>();
                _subfolders.Add(new Folder("F1"));
                _subfolders[0].Subfolders.Add(new Folder("F1.1"));
                _subfolders[0].Subfolders.Add(new Folder("F1.2"));
    
                _subfolders.Add(new Folder("F2"));
                _subfolders[1].Subfolders.Add(new Folder("F2.1"));
                _subfolders[1].Subfolders.Add(new Folder("F2.2"));
            }
    
            public Folder(string name)
            {
                _name = name;
                _subfolders = new List<Folder>();
            }
        }
    
    and I want display instance of this class in TreeView, but after this call TreeView is empty. Does anybode know what is wrong?
            private void Sample()
            {
                Folder folder = new Folder();
    
                FrameworkElementFactory labelFactory = new FrameworkElementFactory(typeof(TextBlock));
    
                //1. We have to define type of items
                HierarchicalDataTemplate template = new HierarchicalDataTemplate(typeof(Folder));
                //2. We have to define source for subitems
                template.ItemsSource = new Binding("Subfolders");
                //3. We have to define DataTemplate for items
                labelFactory.SetBinding(TextBlock.TextProperty, new Binding("Name"));
                template.VisualTree = labelFactory;
    
                treeView1.ItemTemplate = template;
                treeView1.ItemsSource = folder.Subfolders;
            }
    
    Regards

Antworten

  • Freitag, 3. Juli 2009 16:27Mark SalsberyMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    Yes I am sure that Sample() is called :-)
    treeView1 is defined in XAML in Grid and Grid is defined in Window.


    I just realized I DID make a change to your code.....I did it almost subconsciously when I first copied it because I knew it wouldn't work correctly if I didn't change it...

    You need to change any properties you bind to in your data class(es) to "public" instead of "internal".


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
  • Samstag, 4. Juli 2009 11:21kicaj TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    Now it works !
    Thanks !
    • Als Antwort markiertkicaj Samstag, 4. Juli 2009 16:59
    •  

Alle Antworten

  • Donnerstag, 2. Juli 2009 23:45Mark SalsberyMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Your code works for me....are you sure your Sample() method is getting called?

    Where is treeView1 defined?


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
  • Freitag, 3. Juli 2009 07:15kicaj TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

    Yes I am sure that Sample() is called :-)
    treeView1 is defined in XAML in Grid and Grid is defined in Window.

  • Freitag, 3. Juli 2009 16:27Mark SalsberyMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    Yes I am sure that Sample() is called :-)
    treeView1 is defined in XAML in Grid and Grid is defined in Window.


    I just realized I DID make a change to your code.....I did it almost subconsciously when I first copied it because I knew it wouldn't work correctly if I didn't change it...

    You need to change any properties you bind to in your data class(es) to "public" instead of "internal".


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
  • Samstag, 4. Juli 2009 11:21kicaj TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    Now it works !
    Thanks !
    • Als Antwort markiertkicaj Samstag, 4. Juli 2009 16:59
    •  
  • Montag, 13. Juli 2009 05:32NothingToDo TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Will you please explain me how can I create the xaml for this example and please if you have any other example
    it will be better.

    I want to be able to create a complex TreeView with images and text, with different formats for different levels and at the same
    time I don't want to use the namespace in the xaml to define the DataType and the ItemsSource, I want to do the binding programmatically if it is possible.

    Thanks in advance.
  • Mittwoch, 15. Juli 2009 05:08NothingToDo TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Any answer?
  • Mittwoch, 15. Juli 2009 22:47Mark SalsberyMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code
    Will you please explain me how can I create the xaml for this example

    For this example I used

    <TreeView Name="treeView1" />
    



    You may get better forum results if you start a new thread.


    Mark

    Mark Salsbery Microsoft MVP - Visual C++