none
Treeview databinding to a DataTable RRS feed

  • Question

  • 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(); }
     to the datatable tbl on the backend.

    Thursday, January 7, 2010 9:21 PM

Answers

  • Hi,
    Based on my understanding, what you want to do is to bind a single DataTable to a TreeViewItem.

    Because the ItemsSource property of type IEnumerable, we need to set a value of type IEnumerable to this property, in this case, pass the default dataview of the DataTable to this property.

    You need to create a ItemTemplate and bind the data you want in the DataTemplate, the ItemsSource should be a DataView, and you can use the DefaultView provided by DataTable. I have provided a simple solution for you as follows:

    XAML

    <Window x:Class="MyThreads.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <TreeView>
                <TreeViewItem Header="Search List" />
                <TreeViewItem Header="List Items" Margin="15,0,0,0">
                    <TreeViewItem Name="ItemList" Header="Codeset 1">
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Name}"></TextBlock>
                            </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                </TreeViewItem>
            </TreeView>
        </Grid>
    </Window>


    XAML code behind:
            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";
                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);

                //This is working now
                ItemList.ItemsSource = tbl.DefaultView;
            }

    Hope that helps.
    If you have any  question, please feel free to let me know.

    Kevin

    • Marked as answer by Groovebox303 Tuesday, January 12, 2010 12:36 PM
    Monday, January 11, 2010 8:27 AM
    Moderator

All replies

  • Hi,
    Based on my understanding, what you want to do is to bind a single DataTable to a TreeViewItem.

    Because the ItemsSource property of type IEnumerable, we need to set a value of type IEnumerable to this property, in this case, pass the default dataview of the DataTable to this property.

    You need to create a ItemTemplate and bind the data you want in the DataTemplate, the ItemsSource should be a DataView, and you can use the DefaultView provided by DataTable. I have provided a simple solution for you as follows:

    XAML

    <Window x:Class="MyThreads.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <TreeView>
                <TreeViewItem Header="Search List" />
                <TreeViewItem Header="List Items" Margin="15,0,0,0">
                    <TreeViewItem Name="ItemList" Header="Codeset 1">
                        <TreeViewItem.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Name}"></TextBlock>
                            </DataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                </TreeViewItem>
            </TreeView>
        </Grid>
    </Window>


    XAML code behind:
            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";
                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);

                //This is working now
                ItemList.ItemsSource = tbl.DefaultView;
            }

    Hope that helps.
    If you have any  question, please feel free to let me know.

    Kevin

    • Marked as answer by Groovebox303 Tuesday, January 12, 2010 12:36 PM
    Monday, January 11, 2010 8:27 AM
    Moderator
  • Thanks Kevin, that worked, now im stuck on how to show the treeview's contents if its a large treeview through a scroll bar that dynamically adjusts to the treeview contents D'oh!
    Tuesday, January 12, 2010 12:37 PM