Microsoft Developer Network > Página principal de foros > Windows Presentation Foundation (WPF) > HierarchicalDataTemplate for TreeView programmatically (C#).
Formular una preguntaFormular una pregunta
 

RespondidaHierarchicalDataTemplate for TreeView programmatically (C#).

  • jueves, 02 de julio de 2009 18:52kicaj Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    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

Respuestas

  • viernes, 03 de julio de 2009 16:27Mark SalsberyMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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++
  • sábado, 04 de julio de 2009 11:21kicaj Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Now it works !
    Thanks !
    • Marcado como respuestakicaj sábado, 04 de julio de 2009 16:59
    •  

Todas las respuestas

  • jueves, 02 de julio de 2009 23:45Mark SalsberyMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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++
  • viernes, 03 de julio de 2009 7:15kicaj Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

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

  • viernes, 03 de julio de 2009 16:27Mark SalsberyMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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++
  • sábado, 04 de julio de 2009 11:21kicaj Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Now it works !
    Thanks !
    • Marcado como respuestakicaj sábado, 04 de julio de 2009 16:59
    •  
  • lunes, 13 de julio de 2009 5:32NothingToDo Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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.
  • miércoles, 15 de julio de 2009 5:08NothingToDo Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Any answer?
  • miércoles, 15 de julio de 2009 22:47Mark SalsberyMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    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++