Click here to Skip to main content
15,881,172 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Problem with DataGrid Rows in WPF Pin
Mycroft Holmes2-Jul-11 12:40
professionalMycroft Holmes2-Jul-11 12:40 
QuestionQ1: Populate TreeView from source (C#) Pin
Mc_Topaz30-Jun-11 23:34
Mc_Topaz30-Jun-11 23:34 
AnswerRe: Q1: Populate TreeView from source (C#) Pin
Pete O'Hanlon1-Jul-11 0:05
mvePete O'Hanlon1-Jul-11 0:05 
QuestionQ2: Implement TreeView with multi level of data Pin
Mc_Topaz1-Jul-11 1:19
Mc_Topaz1-Jul-11 1:19 
AnswerRe: Q2: Implement TreeView with multi level of data Pin
Pete O'Hanlon1-Jul-11 1:30
mvePete O'Hanlon1-Jul-11 1:30 
GeneralRe: Q2: Implement TreeView with multi level of data Pin
Mc_Topaz1-Jul-11 2:14
Mc_Topaz1-Jul-11 2:14 
GeneralRe: Q2: Implement TreeView with multi level of data Pin
Pete O'Hanlon1-Jul-11 2:22
mvePete O'Hanlon1-Jul-11 2:22 
QuestionQ3: Implement TreeView with two data source classes [modified] Pin
Mc_Topaz3-Jul-11 23:45
Mc_Topaz3-Jul-11 23:45 
I have one further problem concerning displaying items in the TreeView. Adding one more collection for each Person, the Item collection.

The entire C# code is:
namespace TreeView
{
    public partial class MainWindow : Window
    {
        List<Person> persons;

        public MainWindow()
        {
            InitializeComponent();

            persons = new List<Person>();

            Person p1 = new Person("Person1");
            Person c1 = new Person("Children1");

            c1.Items.Add(new Item("Telephone"));
            c1.Items.Add(new Item("Game console"));

            p1.Children.Add(c1);
            p1.Children.Add(new Person("Children2"));

            p1.Items.Add(new Item("Car"));
            p1.Items.Add(new Item("Computer"));

            persons.Add(p1);
            tvTree.ItemsSource = persons;
        }
    }

    public class Person
    {
        List<Person> children = new List<Person>();
        List<Item> items = new List<Item>();
        string name;

        public Person(string name)
        {
            this.name = name;
        }

        public string Name
        {
            get { return name; }
        }

        public List<Person> Children
        {
            get { return children; }
            set { children = value; }
        }

        public List<Item> Items
        {
            get { return items; }
            set { items = value; }
        }
    }

    public class Item
    {
        string type;

        public Item(string type)
        {
            this.type = type;
        }

        public string Type
        {
            get { return type; }
        }
    }
}


Notice the List<item> collection in the Person class. Can I display that collection in the TreeView at the same time as the List<person> Children collection?
How should I implement that in my XAML code?

My current XAML code:
<Window x:Class="TreeView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TreeView"
        Title="MainWindow" Height="350" Width="525">
    
    <Grid>
        <TreeView Name="tvTree" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,5,0,0" Width="300" Height="300">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}"  DataType="{x:Type local:Person}">
                    <Grid>
                        <TextBlock Text="{Binding Name}" />
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>


The result should be something like this:
Person1
   Chilrden1
      Telephone
      Game console
   Chilrden2
   Car
   Computer


modified on Monday, July 4, 2011 7:20 AM

QuestionSync call to WCF Methods Pin
NTheOne30-Jun-11 19:03
NTheOne30-Jun-11 19:03 
AnswerRe: Sync call to WCF Methods Pin
Pete O'Hanlon30-Jun-11 21:41
mvePete O'Hanlon30-Jun-11 21:41 
QuestionCreate event in UserControl Pin
jadughar30-Jun-11 10:55
jadughar30-Jun-11 10:55 
AnswerRe: Create event in UserControl Pin
Mark Salsbery30-Jun-11 13:43
Mark Salsbery30-Jun-11 13:43 
GeneralRe: Create event in UserControl Pin
jadughar30-Jun-11 21:12
jadughar30-Jun-11 21:12 
GeneralRe: Create event in UserControl Pin
Mark Salsbery1-Jul-11 6:26
Mark Salsbery1-Jul-11 6:26 
GeneralRe: Create event in UserControl Pin
jadughar1-Jul-11 7:59
jadughar1-Jul-11 7:59 
GeneralRe: Create event in UserControl Pin
Mark Salsbery1-Jul-11 8:09
Mark Salsbery1-Jul-11 8:09 
GeneralRe: Create event in UserControl Pin
jadughar1-Jul-11 10:45
jadughar1-Jul-11 10:45 
GeneralRe: Create event in UserControl Pin
Mark Salsbery1-Jul-11 12:04
Mark Salsbery1-Jul-11 12:04 
GeneralRe: Create event in UserControl Pin
jadughar2-Jul-11 0:11
jadughar2-Jul-11 0:11 
GeneralRe: Create event in UserControl Pin
Mark Salsbery2-Jul-11 6:19
Mark Salsbery2-Jul-11 6:19 
GeneralRe: Create event in UserControl Pin
jadughar3-Jul-11 22:15
jadughar3-Jul-11 22:15 
QuestionTwo-way binding requires Path or XPath error Pin
SledgeHammer0130-Jun-11 6:46
SledgeHammer0130-Jun-11 6:46 
AnswerRe: Two-way binding requires Path or XPath error Pin
Pete O'Hanlon30-Jun-11 6:56
mvePete O'Hanlon30-Jun-11 6:56 
GeneralRe: Two-way binding requires Path or XPath error Pin
SledgeHammer0130-Jun-11 7:08
SledgeHammer0130-Jun-11 7:08 
GeneralRe: Two-way binding requires Path or XPath error Pin
SledgeHammer0130-Jun-11 9:20
SledgeHammer0130-Jun-11 9:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.