2011-03-22 93 views
6

我试图改变我的TreeView在一个文件夹图标中的图标。另外,当它崩溃时,它需要打开一个文件夹图标。如何将TreeView图标更改为文件夹图标?

我的树形已数据绑定物品,并代码:

<TreeView x:Name="TreeViewCategories" Grid.Row="0" Grid.Column="1" Height="610" HorizontalAlignment="Left" Margin="29,111,0,0" VerticalAlignment="Top" Width="315" BorderThickness="0" Background="Transparent" > 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate ItemsSource="{Binding Items}"> 
      <TextBlock FontSize="20" Text="{Binding Name}" PreviewMouseDown="TextBlock_PreviewMouseDown"/> 
     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView> 

而且我这是怎么填满来自XML项目树形视图(这是一个剪断出来的很多代码:

private void LoadHospitalXML() 
{ 
    try 
    { 
     FileStream fs = new FileStream("ConfigOrgHospital.xml", FileMode.Open, FileAccess.Read); 

     var xml = XmlReader.Create(fs); 

     rootElement = ConvertHospitalData(xml); 

     this.TreeViewCategories.ItemsSource = null; 
     List<HospitalWrapper> li = new List<HospitalWrapper>(); 
     var hosp = rootElement.Items.FirstOrDefault(); 
     if (hosp != null) 
     { 
      foreach (var i in hosp.Hospital) 
      { 
       li.AddIfNotNull(CreateHospList(i)); 
      } 
     } 

     this.TreeViewCategories.ItemsSource = li; 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show(e.Message); 
    } 

} 

private HospitalWrapper CreateHospList(object obj) 
{ 
    var newItem = new HospitalWrapper(); 

    newItem.Context = obj; 
    //Hospital Names// 
    if (obj is HospitalDataHospitalsHospital) 
    { 
     var hosp = (HospitalDataHospitalsHospital)obj; 

     //newItem.Title = "Hospitals"; 
     newItem.Name = hosp.DefaultName; 

     var tmp = new HospitalWrapper(); 
     tmp.Name = "Sites"; 
     tmp.IsTitle = true; 

     if (hosp.Sites != null) 
      foreach (var i in hosp.Sites) 
      { 
       tmp.Items.AddIfNotNull(CreateHospList(i)); 
      } 
     newItem.Items.Add(tmp); 

     tmp = new HospitalWrapper(); 
     tmp.Name = "Specialties"; 
     tmp.IsTitle = true; 

     if (hosp.Deps != null) 
      foreach (var j in hosp.Deps) 
      { 

       tmp.Items.AddIfNotNull(CreateHospList(j)); 
      } 

     newItem.Items.Add(tmp); 
    } 
} 

回答

10

我在前几天做了这样的事情,在我的应用程序中,文件夹图标被添加到HierarchicalDataTemplate中,这些对象的行为与文件夹类似,我使用触发器来根据项目是否展开来更改图标,这里是XAML的相关位:

<HierarchicalDataTemplate DataType="{x:Type data:FeedComposite}" 
           ItemsSource="{Binding Path=Children}"> 
     <StackPanel Orientation="Horizontal" Margin="1"> 
      <StackPanel.Children> 
       <Image> 
        <Image.Style> 
         <Style BasedOn="{StaticResource IconImageStyleSmall}" TargetType="Image"> 
          <Setter Property="Source" Value="{Binding Source={StaticResource Icon_FolderClosed}, Mode=OneTime}"/> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True"> 
            <Setter Property="Source" Value="{Binding Source={StaticResource Icon_FolderOpen}, Mode=OneTime}"/> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </Image.Style> 
       </Image> 
       <TextBlock Text="{Binding Title}"/> 
      </StackPanel.Children> 
     </StackPanel> 
    </HierarchicalDataTemplate> 

{StaticResource Icon_FolderOpen}{StaticResource Icon_FolderClosed}BitmapImages持有的文件夹状态的图标。 IconImageStyleSmall是一种将图像的MaxWidthMaxHeight设置为适当的样式的样式。


编辑:完成的缘故。

<BitmapImage x:Key="Icon_FolderOpen" UriSource="pack://application:,,,/ImageResources/Icons/FolderOpen.ico" /> 
<BitmapImage x:Key="Icon_FolderClosed" UriSource="pack://application:,,,/ImageResources/Icons/FolderClosed.ico" /> 

<Style x:Key="IconImageStyleSmall" TargetType="Image"> 
    <Setter Property="MaxWidth" Value="16"/> 
    <Setter Property="MaxHeight" Value="16"/> 
    <Setter Property="Margin" Value="1"/> 
</Style> 

Icons used