2014-05-15 101 views
0

我使用了令人敬畏的MahAppsMetro WPF控件套件。我有一個加載文件系統的樹視圖。我也想顯示在TreeViewItem像S,所以我已經覆蓋了地鐵樹風格如下WPF TreeView項目的重寫樣式

<UserControl x:Class="GDE.Tree.TreeView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:Caliburn="http://www.caliburnproject.org" 
      xmlns:Metro="http://metro.mahapps.com/winfx/xaml/controls" 
      mc:Ignorable="d" 
      d:DesignHeight="300" 
      d:DesignWidth="300"> 
    <UserControl.Resources> 
     <HierarchicalDataTemplate x:Key="TreeTemplate" ItemsSource="{Binding Path=Children}"/> 
    </UserControl.Resources> 

    <!--TreeView--> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <TextBox Grid.Row="0" 
       Margin="5" 
       IsReadOnly="True" 
       Text="{Binding SelectedPath, Mode=TwoWay}"/> 
     <TreeView Grid.Row="1" 
        Margin="5" 
        ItemsSource="{Binding RootChildren}" 
        ItemTemplate="{StaticResource TreeTemplate}"> 
      <TreeView.Resources> 
       <Style TargetType="{x:Type TreeViewItem}" 
         BasedOn="{StaticResource MetroTreeViewItem}"> 
        <Setter Property="HeaderTemplate"> 
         <Setter.Value> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal"> 
            <Image Name="img" 
              Width="16" 
              Height="16" 
              Stretch="Fill" 
              Source="{Binding Path=Icon, Mode=OneTime}" 
              VerticalAlignment="Center" 
              HorizontalAlignment="Center"/> 
            <TextBlock Text="{Binding DisplayName, Mode=OneTime}" 
               Margin="5,0,0,0" 
               VerticalAlignment="Center" 
               HorizontalAlignment="Left"/> 
           </StackPanel> 
          </DataTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </TreeView.Resources> 
     </TreeView> 
    </Grid> 
</UserControl> 

但這似乎並不如預期的工作,我想不通爲什麼。從這個XAML,我沒有得到任何綁定錯誤和視覺效果看起來像:

TreeView

我已經更廣泛的與XAML標記,但我得到完全相同的視覺效果。 如何更改上述XAML以提供Image/TextBlocks以及MahApps外觀?

謝謝你的時間。

+0

你嘗試從'DataTemplate'移動'StackPanel'直接到你的'HierarchicalDataTemplate'而不是覆蓋'Style'? – dkozl

+0

Arrrhhhhhhh!不,我現在就試試這個......笨蛋。我認爲這可能是答案。 – MoonKnight

+0

這就是答案。非常感謝您在這裏的時間。你可以寫答案,或者我會刪除這個? – MoonKnight

回答

2

綜上所述意見,而不是改變Style你需要從DataTemplate直接移動到StackPanel作爲HierarchicalDataTemplate在使用模板的時刻沒有內容:

<HierarchicalDataTemplate x:Key="TreeTemplate" ItemsSource="{Binding Path=Children}"> 
    <StackPanel Orientation="Horizontal"> 
     <Image Name="img" ... /> 
     <TextBlock Text="{Binding DisplayName, Mode=OneTime}" ... /> 
    </StackPanel> 
</HierarchicalDataTemplate> 
+0

感謝您的提問。還有一件事是爲upvote和answer ...請參閱編輯。 – MoonKnight

+0

你的編輯出現並消失。它有用嗎? – dkozl

+0

我一直很傻。不是好日子... – MoonKnight