簡體   English   中英

覆蓋WPF TreeView項的樣式

[英]Overriding Style of WPF TreeView Items

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

<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的外觀和感覺?

謝謝你的時間。

要總結注釋而不是更改Style您需要將StackPanelDataTemplate直接移動到HierarchicalDataTemplate因為目前使用的模板沒有內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM