Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行时wpf treeviewitem设置图标_Wpf - Fatal编程技术网

运行时wpf treeviewitem设置图标

运行时wpf treeviewitem设置图标,wpf,Wpf,我有一个树状视图,格式如下: <TreeView x:Name="treview_Menu"/> 在运行时,我有: dim oList as list(of CustomClass) = CustomClass_LayerLink.ListAll() dim node as TreeViewItem for each oEntity as CustomClass in oList node = new TreeViewItem with { .header

我有一个树状视图,格式如下:

<TreeView x:Name="treview_Menu"/>
在运行时,我有:

dim oList as list(of CustomClass) = CustomClass_LayerLink.ListAll()
dim node as TreeViewItem

for each oEntity as CustomClass in oList
   node = new TreeViewItem with
   {
      .header = oEntity.description
   }

   me.treview_Menu.items.add(node)
next
如何为添加的每个节点设置图标? 谢谢


我使用:visual studio ultimate 2012和wpf。

您可以绑定图像的源属性。绑定应该可以工作,因此您不需要for-each循环在wpf中手动完成绑定

希望能有帮助

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Name="icon"
                           Width="15"
                           Height="15"
                           Source="{Binding icon}"/>
                    <TextBlock Text="{Binding description}"  />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>


Icon是绑定到Customclass的Icon属性的图像,它应该是图像的路径HI,感谢帮助,它可以工作,但是现在我如何在双击TreeView项目时启动特定的windown?
<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Name="icon"
                           Width="15"
                           Height="15"
                           Source="{Binding icon}"/>
                    <TextBlock Text="{Binding description}"  />
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>