WPF C#如何在treeview中添加带有图标的文本 [英] WPF C# How to add text with icons in treeview

查看:202
本文介绍了WPF C#如何在treeview中添加带有图标的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我正在开发WPF中的第一个应用程序,我有一个treeview元素。  我正在添加带有texblock和按钮的新父节点。这是我的代码:

I'm developing my first app in WPF and i have a treeview element.  I'm adding new parent nodes with texblock and button. Here is the code that i have:

private void AddLocButton(object sender, RoutedEventArgs e)
        {
            if (textbox1.Text.Trim().Length != 0)
            {

                TreeViewItem newChild = new TreeViewItem();
                newChild.Header = textbox1.Text;
                ThreeView1.Items.Add(newChild);

            }
            else
            {
                MessageBox.Show("Please fill textbox.");

            }

        }


我需要的是在树视图中的所有级别节点中添加相同的图标。图标应位于我使用textbox1和按钮添加的节点名称旁边。我该怎么做?

What i need is to add the same icons in all this level nodes in treeview. The icons should be next to the node name that i added with textbox1 and button. How can I do that?

推荐答案

你好,

该代码是一种在Windows窗体中可行的方法,但是使用wpf是一种不好的方法。

That code is an approach which would be fine in windows forms but is a bad way to go with wpf.

与WPF一起我很强烈建议你学习MVVM模式。

Along with WPF I strongly advise you to learn the MVVM pattern.

如果使用代码添加控件,你会发现使用treeview之类的工作非常努力。

You will find working with the likes of a treeview very hard work if you use code to add controls.

mvvm方法非常不同。如果你习惯了它,那么最初它似乎很奇怪。

The mvvm approach is very different. If you're used to winforms it'll seem strange initially.

我接近这个的方法是使用绑定和模板。

The way I'd approach this is using binding and templating.

treeview是一种奇怪的项目控件。其中的每个孩子都包含一个带标题的物品控件

The treeview is an odd sort of an itemscontrol. Each of it's children contain a headered itemscontrol

https://msdn.microsoft.com/en-us/library/system.windows.controls.headereditemscontrol(v = vs.110).aspx

这就是为什么你在该代码中操作一个名为Header的东西。

Which is why you're manipulating something called a Header in that code.

绑定一个observablecollection< t>树视图的itemssource。

Bind an observablecollection<t> to the itemssource of the treeview.

使用hierarchicaldatatemplete(s)将对象及其子项转换为treeviewitems。

Use hierarchicaldatatemplete(s) to turn the objects and their children into treeviewitems.

https://blogs.msdn.microsoft.com/mikehillberg/2009/10 / 30 / treeview-and-hierarchicaldatatemplate-step-by-step /

https://msdn.microsoft.com/en-us/library/system.windows.hierarchicaldatatemplate(v=vs.110).aspx

我不得不去google的缓存版本来获取msdn文章中的代码:

I had to go to google's cached version to get the code from that msdn article:

<Window x:Class="SDKSample.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="HierarchicalDataTemplate Sample"
  xmlns:src="clr-namespace:SDKSample">
  <DockPanel>
    <DockPanel.Resources>
      <src:ListLeagueList x:Key="MyList"/>

      <HierarchicalDataTemplate DataType    = "{x:Type src:League}"
                                ItemsSource = "{Binding Path=Divisions}">
        <TextBlock Text="{Binding Path=Name}"/>
      </HierarchicalDataTemplate>

      <HierarchicalDataTemplate DataType    = "{x:Type src:Division}"
                                ItemsSource = "{Binding Path=Teams}">
        <TextBlock Text="{Binding Path=Name}"/>
      </HierarchicalDataTemplate>

      <DataTemplate DataType="{x:Type src:Team}">
        <TextBlock Text="{Binding Path=Name}"/>
      </DataTemplate>
    </DockPanel.Resources>

    <Menu Name="menu1" DockPanel.Dock="Top" Margin="10,10,10,10">
        <MenuItem Header="My Soccer Leagues"
                  ItemsSource="{Binding Source={StaticResource MyList}}" />
    </Menu>

    <TreeView>
      <TreeViewItem ItemsSource="{Binding Source={StaticResource MyList}}" Header="My Soccer Leagues" />
    </TreeView>

  </DockPanel>
</Window>

我不会将您的列表放在静态资源中,而是将其公开为viewmodel的公共属性。

Rather than putting your list in a staticresource, I would expose it as a public property of a viewmodel.

与其工作方式类似:

https://social.technet.microsoft.com/wiki/contents/articles/32164.wpf-mvvm-step-by-step-2.aspx


这篇关于WPF C#如何在treeview中添加带有图标的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆