0
我是WPF的新手,非常感謝我遇到的難題。我需要在WPF TreeView
的主應用菜單中綁定TreeViewItem
。我需要的是將單個數據表綁定到TreeViewItem
(XAML和代碼隱藏)的正確語法,這是迄今爲止我所擁有的。請提供您的建議代碼,您的幫助表示讚賞。爲了讓事情再次澄清,我只是試圖綁定ItemList
TreeViewItem
的ItemsSource
。該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();
}