我使用了令人敬畏的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,我沒有得到任何綁定錯誤和視覺效果看起來像:
我已經更廣泛的與XAML標記,但我得到完全相同的視覺效果。 如何更改上述XAML以提供Image/TextBlocks以及MahApps外觀?
謝謝你的時間。
你嘗試從'DataTemplate'移動'StackPanel'直接到你的'HierarchicalDataTemplate'而不是覆蓋'Style'? – dkozl
Arrrhhhhhhh!不,我現在就試試這個......笨蛋。我認爲這可能是答案。 – MoonKnight
這就是答案。非常感謝您在這裏的時間。你可以寫答案,或者我會刪除這個? – MoonKnight