2011-11-25 89 views
2

我需要將圖像添加到WPF treeview節點,我看過這個例子How do I add icons next to the nodes in a WPF TreeView?,它的工作正常,除了所有節點都有相同的圖像。我想所有的樹形視圖中沒有任何子節點的節點要麼沒有圖像,要麼具有不同的圖像。不同的WPF Treeview圖標取決於節點的類型

這是我的XAML,我設置圖像:

<HierarchicalDataTemplate x:Key="NodeTemplate"> 
     <StackPanel Orientation="Horizontal" Margin="2"> 
      <Image Source="test.png" Width="16" Height="16" SnapsToDevicePixels="True"/> 
     <TextBlock x:Name="tb"/> 
     </StackPanel> 
     <HierarchicalDataTemplate.ItemsSource> 
      <Binding> 
       <Binding.XPath>child::node()</Binding.XPath> 
      </Binding> 
     </HierarchicalDataTemplate.ItemsSource> 
     <HierarchicalDataTemplate.Triggers> 
      <DataTrigger Binding="{Binding Path=NodeType}" Value="Text"> 
       <Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding Path=NodeType}" Value="Element"> 
       <Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter> 
      </DataTrigger> 
     </HierarchicalDataTemplate.Triggers> 
    </HierarchicalDataTemplate> 

下面是輸出的屏幕截圖

Treeview output 可能有人請建議我怎麼能做到這一點,可能的解決方案可以要麼改變XAML,要麼通過C#編程。

回答

4

下面是我用來解決幾乎相同的問題的一些代碼。 (I設計了這個對數據是XML數據,所以的XPath =「@名稱」指的是節點的屬性名稱的值,而名稱意味着元素類型。)

<Window x:Class="NodeExplorer2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:my="clr-namespace:NodeExplorer2"> 
    <Window.Resources> 
     <my:PathConverter x:Key="iconConverter"/> 

     <HierarchicalDataTemplate x:Key="XmlTreeTemplate"> 
      <HierarchicalDataTemplate.ItemsSource> 
       <Binding XPath="child::node()" /> 
      </HierarchicalDataTemplate.ItemsSource> 

      <StackPanel Orientation="Horizontal"> 
       <Image x:Name="icon" SnapsToDevicePixels="True" Stretch="None" Margin="0,0,3,0" /> 
       <TextBlock Text={Binding XPath="@name"/> 
      </StackPanel> 
      <HierarchicalDataTemplate.Triggers> 
       <DataTrigger Binding="{Binding Path=NodeType}" Value="Element"> 
        <Setter TargetName="icon" Property="Source"> 
         <Setter.Value> 
          <Binding Path="Name" Converter="{StaticResource iconConverter}"> 
           <Binding.FallbackValue> 
            <ImageSource> 
             Data/Icons/unknown.png 
            </ImageSource> 
           </Binding.FallbackValue> 
          </Binding> 
         </Setter.Value> 
        </Setter> 
       </DataTrigger> 
      </HierarchicalDataTemplate.Triggers> 
     </HierarchicalDataTemplate> 

轉換器:

public class PathConverter: IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     //Console.WriteLine("Value:" + value); 
      return "Data/Icons/" + value + ".png"; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return ""; 
    } 
} 
+0

您好AkselK,我很新的WPF能請你解釋一下和xaml必須放置在哪裏?我把它放在標籤,但即時獲取以下編譯時錯誤 - 「我的」是一個未聲明的前綴 –

+0

啊,我明白了。 my:是一個名稱空間,它在xaml頂部的標記中聲明。在我的情況下,我已經聲明我的:xmlns:my =「clr-namespace:NodeExplorer2」就你而言,它將是xmlns:my =「clr-namespace:Your_project_name」。上述所有代碼都在您的第一個實際組件(可能爲)之上的中聲明。 my:PathConverter意味着我聲明瞭一個PathConverter類型的對象,而x:Key = iconConverter意味着我可以通過{StaticResource iconConverter}來引用它。 – AkselK

+0

很酷,非常感謝你的解釋!我只需要添加一些方法來檢查一個特定的節點是否有任何孩子,如果沒有,那麼我將節點圖像更改爲一個孩子圖標......標記爲已回答 –