在运行时向 WPF TreeViewItem 添加图标
有许多示例在 XAML 中演示了这一点,如下所示:
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="..."/>
<TextBlock>Hello</TextBlock>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
但我需要在运行时代码中执行此操作 - TreeView 的目的是显示计算机上的文件和文件夹。
所以我不确定如何在代码中使用 Header:
For Each f In directory.GetFiles()
Dim icon = System.Drawing.Icon.ExtractAssociatedIcon(f.FullName)
Dim name As String = f.Name
Dim item As New TreeViewItem
item.Header = ...
Next
任何人都可以演示这个概念吗?
编辑:我想我明白了,我应该使用水平 StackPanel 和两个单独的控件 - TextBlock 和 Image。这是正确的方法吗?
There are many samples demonstrating this in XAML, such as the following:
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Source="..."/>
<TextBlock>Hello</TextBlock>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
But I need to do this in runtime code - the purpose of TreeView is to show files and folders on the computer.
So I'm not sure how to work with Header in code:
For Each f In directory.GetFiles()
Dim icon = System.Drawing.Icon.ExtractAssociatedIcon(f.FullName)
Dim name As String = f.Name
Dim item As New TreeViewItem
item.Header = ...
Next
Can anyone demonstrate the concept please?
EDIT: I think I'm getting it, I should use horizontal StackPanel with two separate controls - TextBlock and Image. Is this the right approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
。
以下是有关如何开始的示例代码。首先了解这一点,然后进行适当的更改以满足您的需求。用 C# 和 XAML 编写的代码。希望您能够理解 C# 并能够将其转换为 Basic。
这是 XAML 代码:
希望这个示例代码会对您有很大帮助。 :-)
。
.
Here is the sample code as to how you should start. Understand this first, and then make appropriate change to meet your need. The code written in C# and XAML. Hope you'll understand C# and would be able to convert it to Basic.
And here is the XAML code:
Hope, this sample code would help you greatly. :-)
.