Simple WPF Treeview binding
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Simple WPF Treeview binding

  1. #1
    Join Date
    Jan 2010
    Posts
    3

    Unhappy Simple WPF Treeview binding

    Hi,
    I am new to WPF and would really appreciate help on an issue Im struggling with. I need to Bind a treeview Item in a WPF treeview in the main app's Menu. All i need is the correct syntax to bind the single datatable to treeview item(XAML and the codebehind) this is what I have so far. Please provide code with your suggestion, your help is appreciated. To make things clear once again I am just trying to Bind the "ItemList" treeview Item's Itemsource. Your helps appreciated

    XAML


    <Window x:Class="WPFToolkit.Window1"



    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"



    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"



    xmlns:local="clr-namespace:WPFToolkit"



    Title="Window1" Height="300" Width="300">





    <Window.Resources>



    </Window.Resources>



    <Grid>



    <TreeView Name="SampleTree" Margin="-9,0,0,0">



    <TreeViewItem Header="Create List"



    MouseDoubleClick="CreateCodeset_Click" IsEnabled="True"/>



    <TreeViewItem Header="Search List"



    MouseDoubleClick="SearchCodeSet_Click" />



    <TreeViewItem Header="List Items" Margin="15,0,0,0">



    <TreeViewItem Name="ItemList" Header="Codeset 1" Style="{StaticResource listMenuItem}">



    </TreeViewItem>



    </TreeViewItem>



    </TreeView>



    </Grid>



    </Window>


    C#



    public void CreateList()

    {


    DataTable tbl = new DataTable("Items");

    tbl.Columns.Add("Name");


    DataRow row = tbl.NewRow();

    row["Name"] = "Fruits";

    tbl.Rows.Add(row);

    DataRow row2 = tbl.NewRow();

    row["Name"] = "Vegetables";

    tbl.Rows.Add(row2);

    DataRow row3 = tbl.NewRow();

    row["Name"] = "Meats";

    tbl.Rows.Add(row3);

    DataRow row4 = tbl.NewRow();

    row["Name"] = "Drinks";

    tbl.Rows.Add(row4);

    DataRow row5 = tbl.NewRow();

    row["Name"] = "Bread";

    tbl.Rows.Add(row5);

    //This is not working
    ItemList.IItemsSource = tbl.Select();

    }

  2. #2
    Join Date
    Jan 2010
    Posts
    3

    Re: Simple WPF Treeview binding

    I figured it out thanks to some help , I'll post this in case it helps someone else out as well.

    This is what I had to add in the xaml

    <Window.Resources>
    <DataTemplate x:Key="itemsTemplate">
    <StackPanel>
    <TextBlock Text="{Binding Path=Name}"/>
    </StackPanel>
    </DataTemplate>
    </Window.Resources>

    then in the treeview:

    <TreeViewItem Name="Products" Header="Product List"
    ItemTemplate="{StaticResource itemsTemplate}"/>

    And finally in the code behind: C#

    public void CreateList()
    {
    DataTable tbl = new DataTable("Items");
    tbl.Columns.Add("Name");

    DataRow row = tbl.NewRow();
    row["Name"] = "Fruits";
    tbl.Rows.Add(row);

    DataRow row2 = tbl.NewRow();
    row2["Name"] = "Vegetables"; // original code has "row" here
    tbl.Rows.Add(row2);

    DataRow row3 = tbl.NewRow();
    row3["Name"] = "Meats";
    tbl.Rows.Add(row3);

    DataRow row4 = tbl.NewRow();
    row4["Name"] = "Drinks";
    tbl.Rows.Add(row4);

    DataRow row5 = tbl.NewRow();
    row5["Name"] = "Bread";
    tbl.Rows.Add(row5);

    //Create a Dataview from the DataTable
    DataView dv = new DataView(tbl);

    //Finally sort the list
    dv.Sort = "Name";
    Products.ItemsSource = dv;



    }

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,956

    Re: Simple WPF Treeview binding

    Did you see my response in your other post?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center