【问题标题】:How to add icons to wpf treeview using dockpanel and binding?如何使用停靠面板和绑定将图标添加到 wpf 树视图?
【发布时间】:2012-09-26 20:02:53
【问题描述】:

我在停靠面板中有一个树视图,树视图的所有元素都在 HierarchicalDataTemplates 中。代码如下:

<DockPanel Margin="10,10,0,0" VerticalAlignment="Stretch" Grid.Row="0" Grid.RowSpan="5" Grid.Column="0">
        <DockPanel.Resources>

            <src:TreeViewFilter x:Key="MyList" />

            <HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}">
                <TextBlock Text="{Binding Path=Name}" FontSize="24"/>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}">
                <TextBlock Text="{Binding Path=NameAndCount}" FontSize="16"/>
            </HierarchicalDataTemplate>

        </DockPanel.Resources>
        <TreeView Name="treeView1" BorderThickness="2" ItemsSource="{Binding Source={StaticResource MyList}, UpdateSourceTrigger=PropertyChanged}" TreeViewItem.Selected="treeViewFilter"/>
    </DockPanel>

如您所见,DockPanel 环绕着 TreeView。我要添加的图标将位于第二个 HierarchicalDataTemplate 中,它绑定到字符串并显示为文本框。根据“NameAndCount”的名称,我会选择一个图标显示在左侧。

对我的示例的解决方案有任何想法吗?还是我需要考虑使用不同的模板,例如 StackPanel?

【问题讨论】:

    标签: wpf binding treeview icons dockpanel


    【解决方案1】:

    在任意位置添加 Image-Control,将其 Source 绑定到“NameAndCount”并使用 IValueConverter 将其转换为图像路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      相关资源
      最近更新 更多