tags:

views:

308

answers:

2

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. The treeview shows the 5 rows in the table, but it shows "System.Data.Datarow" instead of the real value, 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.ItemsSource = tbl.Select();
}
A: 

I've made some changes to your xaml and CreateList, pls check if it's working for you

xaml: I've removed Style=".." and set ItemTemplate to a datatemplate defined in window resources section. TextBlock is bent to the Name field of your dataset

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

<Grid>
    <TreeView Height="277" HorizontalAlignment="Left" 
              Margin="64,24,0,0" Name="treeView1" 
              VerticalAlignment="Top" Width="268">
        <TreeViewItem Header="Create List" IsEnabled="True"/>
        <TreeViewItem Header="Search List" />
        <TreeViewItem Header="List Items" Margin="15,0,0,0" IsExpanded="true">
            <TreeViewItem Name="ItemList" Header="Codeset 1" IsExpanded="true"                               
                          ItemTemplate="{StaticResource itemsTemplate}"/>
        </TreeViewItem>
    </TreeView>
</Grid>

CreateList: I've changed ItemList.ItemsSource to a DataView object; you also were assigning "Name" item to the same row object, looks like a bug in your sample code.

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);

//ItemList.ItemsSource = tbl.Select();
ItemList.ItemsSource = new DataView(tbl);

hope this helps, regards

serge_gubenko
A: 

Thanks a lot serge_gubenko, I really had to get this done man, it worked, I didnt know I had to declare a datatemplate to bind it. Thanks again.

Groovebox