MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > HierarchicalDataTemplate for TreeView programmatically (C#).
發問發問
 

已答覆HierarchicalDataTemplate for TreeView programmatically (C#).

  • Thursday, 2 July, 2009 18:52kicaj 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    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

解答

  • Friday, 3 July, 2009 16:27Mark SalsberyMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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++
  • Saturday, 4 July, 2009 11:21kicaj 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    Now it works !
    Thanks !
    • 已標示為解答kicaj Saturday, 4 July, 2009 16:59
    •  

所有回覆

  • Thursday, 2 July, 2009 23:45Mark SalsberyMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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++
  • Friday, 3 July, 2009 7:15kicaj 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

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

  • Friday, 3 July, 2009 16:27Mark SalsberyMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    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++
  • Saturday, 4 July, 2009 11:21kicaj 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    Now it works !
    Thanks !
    • 已標示為解答kicaj Saturday, 4 July, 2009 16:59
    •  
  • Monday, 13 July, 2009 5:32NothingToDo 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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.
  • Wednesday, 15 July, 2009 5:08NothingToDo 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Any answer?
  • Wednesday, 15 July, 2009 22:47Mark SalsberyMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    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++