Thomas
Thomas

Reputation: 34218

TreeView & ListView WPF

suppose i have bind my treeview and plus sign is coming and i want when i will click on the plus sign then dynamically a listview or grid will show just under the node where i will click and related data will show through grid or listview.

in MS-Access when we create relation in table then when we see the master table then related records show a plus sign and when we click on plus sign then related data show just under the plus sign in a grid. the same thing i want to achieve in WPF. please help me with sample code if possible. please do not refer me for any hierarchical Grid in WPF. my output would be different means when i will click on plus sign of the treeview then dynamically a grid or listview will show just under that node. thanks.

Upvotes: 2

Views: 1466

Answers (2)

Vinoth Kumar J
Vinoth Kumar J

Reputation: 167

You can achieve this by

<Window.Resources>
    <DataTemplate DataType="{x:Type local:Student}">
        <TreeView>
            <TreeViewItem Header="{Binding Name}" ItemsSource="{Binding Marks}"></TreeViewItem>
        </TreeView>
    </DataTemplate>
</Window.Resources>
<Grid>        
    <TreeView>
        <TreeViewItem Header="Master">
            <ListBox ItemsSource="{Binding Students}">                    
            </ListBox>
        </TreeViewItem>
    </TreeView>
</Grid>

And In Code Behind

public class Master
{
    public ObservableCollection<Student> Students { get; set; }
    public Master()
    {
        Students = new ObservableCollection<Student>();
        Students.Add(new Student() { Name = "Vinoth", Marks = new ObservableCollection<double> { 90, 95, 100, 80, 70 } });
        Students.Add(new Student() { Name = "Kumar", Marks = new ObservableCollection<double> { 50, 80, 60, 10, 20 } });
        Students.Add(new Student() { Name = "Sparrow", Marks = new ObservableCollection<double> { 70, 50, 72, 78, 89 } });
    }
}

public class Student
{
    public string Name { get; set; }
    public ObservableCollection<double> Marks { get; set; }
}

Refer the screenshotTreeview/ListBox

Upvotes: 0

Constanta
Constanta

Reputation: 399

If I understand correctly what you are trying to do you need to create a CustomControl for this purpose. I'd use Treeview to inherit from. Take a look at its ControlTemplate : http://msdn.microsoft.com/en-us/library/ms788727.aspx#example

Upvotes: 1

Related Questions