在运行时向 WPF TreeViewItem 添加图标

发布于 2024-10-05 06:33:39 字数 744 浏览 0 评论 0原文

有许多示例在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一刻暧昧 2024-10-12 06:33:39

以下是有关如何开始的示例代码。首先了解这一点,然后进行适当的更改以满足您的需求。用 C# 和 XAML 编写的代码。希望您能够理解 C# 并能够将其转换为 Basic。

    public class NameIconPair
    {
        public String Name { get; set; }
        public BitmapSource IconSource { get; set; }
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            var files = System.IO.Directory.GetFiles("E:\\");
            ObservableCollection<NameIconPair> pairs = new ObservableCollection<NameIconPair>();
            foreach (string file in files)
            {
                System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(file);
                Stream stream = new MemoryStream();
                icon.Save(stream);
                BitmapDecoder decoder = IconBitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
                BitmapSource src = decoder.Frames[0];
                pairs.Add(new NameIconPair() { Name = file,  IconSource = src });
            }
            this.DataContext = pairs;
        }
    }

这是 XAML 代码:

    <TreeView ItemsSource="{Binding}">
        <TreeView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding IconSource}"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

希望这个示例代码会对您有很大帮助。 :-)

.

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.

    public class NameIconPair
    {
        public String Name { get; set; }
        public BitmapSource IconSource { get; set; }
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            var files = System.IO.Directory.GetFiles("E:\\");
            ObservableCollection<NameIconPair> pairs = new ObservableCollection<NameIconPair>();
            foreach (string file in files)
            {
                System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(file);
                Stream stream = new MemoryStream();
                icon.Save(stream);
                BitmapDecoder decoder = IconBitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
                BitmapSource src = decoder.Frames[0];
                pairs.Add(new NameIconPair() { Name = file,  IconSource = src });
            }
            this.DataContext = pairs;
        }
    }

And here is the XAML code:

    <TreeView ItemsSource="{Binding}">
        <TreeView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding IconSource}"/>
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

Hope, this sample code would help you greatly. :-)

.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文