2010-01-07 99 views
0

我是WPF的新手,非常感謝我遇到的難題。我需要在WPF TreeView的主應用菜單中綁定TreeViewItem。我需要的是將單個數據表綁定到TreeViewItem(XAML和代碼隱藏)的正確語法,這是迄今爲止我所擁有的。請提供您的建議代碼,您的幫助表示讚賞。爲了讓事情再次澄清,我只是試圖綁定ItemListTreeViewItemItemsSource。該TreeView顯示在表中的5行,但它表明System.Data.DataRow,而不是真正的價值,你的幫助讚賞樹形數據綁定到數據表

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

回答

1

我已經做了一些改動你的XAML和CreateList,請檢查它是否適用於你

xaml:我已經REM ved Style =「..」並將ItemTemplate設置爲窗口資源部分中定義的數據模板。 TextBlock的彎曲,以你的數據集的名稱字段

<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:我已經改變ItemList.ItemsSource到DataView對象;您還將「名稱」項分配給同一行對象,看起來像示例代碼中的錯誤。

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

希望這會有所幫助,至於