TREEVIEW 的 XAML 代码 [英] XAML Code for TREEVIEW
问题描述
以下是我为生成树视图层次结构而编写的代码,
Following is the code I wrote for generating treeview hierarchy,
For Each k As KeyValuePair(Of String, GenreSet) In GenreSetDictionary
Dim t As New TreeNodeSet
t.Genre = True
t.Imagepath = k.Value.IconPath
t.Namee = k.Key
Dim pnode As New TreeViewItem
pnode.DataContext = t
pnode.Visibility = True
For Each z As DatabaseDearDataSet.DiskListRow In adpt.GetDataByGenre(t.Namee)
Dim tt As New TreeNodeSet
tt.Genre = False
tt.Imagepath = IconDictionary(z.DiskIcon).IconPath
tt.Namee = z.DiskName
Dim cnode As New TreeViewItem
cnode.DataContext = tt
pnode.Items.Add(cnode)
Next
DisksTreeView1.Items.Add(pnode)
Next
以下是我在 XAML 中使用的代码:
Following is the code I have used in XAML:
<TreeView Height="211" HorizontalAlignment="Left" Margin="19,15,0,0" Name="TreeView1" VerticalAlignment="Top" Width="346">
<TreeView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImagePath}" Width="32" Height="32"/>
<TextBlock Text="{Binding Namee}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
但是,我无法完成这项工作,请告诉我我的 XAML 哪里出错了.
However, I was not able to get that work, could you please tell me where my XAML went wrong please.
推荐答案
我在这里看到一些小的不匹配:TreeView
控件的名称(TreeView1 或 DisksTreeView1)和 ImagePath
属性(或 Imagepath
,c# 对变量的寄存器很敏感).
I see few minor mismatches here: the name of the TreeView
control (TreeView1 or DisksTreeView1) and the ImagePath
property (or Imagepath
, c# is sensible to the register of variables).
但不正确行为的主要原因是 ItemTemplate
属性应用于 ItemsSource
属性,而不是 Items
属性.
But the main reason of the incorrect behavior is that the ItemTemplate
property is applied to the ItemsSource
property, not to the Items
property.
这里有两种可能的方法来更正代码:
Here are two possible ways to correct the code:
1) 修复数据类、项目模板和绑定到 ItemsSource
1) Fixeing of the data class, item template and binding to the ItemsSource
- 创建
ObservableCollection(Of TreeNodeSet)
类型的myObservableCollection
私有字段. - 向构造函数添加行
DisksTreeView1.ItemsSource = myObservableCollection
- 将
DisksTreeView1.Items.Add(pnode)
行更改为myObservableCollection.Add(t)
行. - 在
TreeNodeSet
类中添加Disks
属性(类型也是ObservableCollection
) - 在 xaml 中,将带有
DataTemplate
的行替换为<HierarchicalDataTemplate ItemsSource="{Binding Disks}"
- 将
pnode.Items.Add(cnode)
行更改为t.Disks.Add(tt)
行.
- Create the
myObservableCollection
private field of the typeObservableCollection(Of TreeNodeSet)
. - Add to the constructor the line
DisksTreeView1.ItemsSource = myObservableCollection
- Change the line
DisksTreeView1.Items.Add(pnode)
to the linemyObservableCollection.Add(t)
. - Add the
Disks
property to theTreeNodeSet
class (the type isObservableCollection
too) - In the xaml replace the line with
DataTemplate
to the line<HierarchicalDataTemplate ItemsSource="{Binding Disks}"
- Change the line
pnode.Items.Add(cnode)
to the linet.Disks.Add(tt)
.
2) 使用 HeaderTemplate
属性而不是 ItemTemplate
属性.
2) Using the HeaderTemplate
property instead of the ItemTemplate
property.
首先,将 DataTemplate 移动到资源中并添加一些键.然后在代码隐藏中的每个 TreeViewItem
附近添加一个类似的代码:
At first, move the DataTemplate to resources and add some key. Then add a similar code near each TreeViewItem
in the code-behind:
Dim pnode As New TreeViewItem
pnode.DataContext = t
pnode.Header = t
pnode.HeaderTemplate = Resources("someKey")
这篇关于TREEVIEW 的 XAML 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!