TreeView没有显示所有节点的图标

时间:2015-05-28 13:03:34

标签: .net wpf xaml controltemplate

有人可以帮我弄清楚我的模板有什么问题吗?我修改了默认模板,因为我希望根据每个节点的Tag属性值显示树节点的不同图标。它仅显示根节点及其直接第一个子节点的图标。所有其余节点都显示没有图标:

<Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TreeViewItem">
                <StackPanel>
                    <Grid Margin="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" MinWidth="16" />
                            <ColumnDefinition Width="Auto" MinWidth="20" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" MinWidth="20" />
                            <ColumnDefinition Width="Auto" MinWidth="20" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <ToggleButton IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" Name="Expander">
                            <ToggleButton.Style>
                                <Style TargetType="ToggleButton">
                                    <Setter Property="UIElement.Focusable" Value="false" />
                                    <Setter Property="FrameworkElement.Width" Value="16" />
                                    <Setter Property="FrameworkElement.Height" Value="16" />
                                    <Setter Property="Control.Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="ToggleButton">
                                                <Border Padding="5,5,5,5" Background="#00FFFFFF" Width="16" Height="16">
                                                    <Path Fill="#00FFFFFF" Stroke="#FF989898" Name="ExpandPath">
                                                        <Path.Data>
                                                            <PathGeometry Figures="M0,0L0,6L6,0z" />
                                                        </Path.Data>
                                                        <Path.RenderTransform>
                                                            <RotateTransform Angle="135" CenterX="3" CenterY="3" />
                                                        </Path.RenderTransform>
                                                    </Path>
                                                </Border>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="UIElement.IsMouseOver" Value="True">
                                                        <Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF1BBBFA" />
                                                        <Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#00FFFFFF" />
                                                    </Trigger>
                                                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                                                        <Setter TargetName="ExpandPath" Property="UIElement.RenderTransform">
                                                            <Setter.Value>
                                                                <RotateTransform Angle="180" CenterX="3" CenterY="3" />
                                                            </Setter.Value>
                                                        </Setter>
                                                        <Setter TargetName="ExpandPath" Property="Shape.Fill" Value="#FF595959" />
                                                        <Setter TargetName="ExpandPath" Property="Shape.Stroke" Value="#FF262626" />
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ToggleButton.Style>
                        </ToggleButton>

                        <ContentPresenter Grid.Column="1">
                            <ContentPresenter.Style>
                                <Style TargetType="{x:Type ContentPresenter}">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=Tag}" Value="2">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Ellipse Width="6" Height="6" Fill="Black" Stroke="Black" />
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=Tag}" Value="1">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Image Width="16" Height="16" Stretch="Uniform" Grid.Column="1" Source="..\Resources\1432599157_layers.png" />
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>                                        
                                    </Style.Triggers>
                                </Style>
                            </ContentPresenter.Style>
                        </ContentPresenter>

                        <Border x:Name="Bd" HorizontalAlignment="Stretch" BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" 
                                Padding="{TemplateBinding Control.Padding}" SnapsToDevicePixels="True" Grid.Column="2" Grid.ColumnSpan="3">
                            <ContentPresenter x:Name="PART_Header"
                                Content="{TemplateBinding HeaderedContentControl.Header}"
                                ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}"
                                ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}"
                                ContentTemplateSelector="{TemplateBinding HeaderedItemsControl.HeaderTemplateSelector}"
                                ContentSource="Header"
                                HorizontalAlignment="Left"
                                SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </Border>
                        <ItemsPresenter HorizontalAlignment="Stretch" x:Name="ItemsHost" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="3" />
                    </Grid>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="TreeViewItem.IsExpanded" Value="False">
                        <Setter TargetName="ItemsHost" Property="UIElement.Visibility" Value="Collapsed" />
                    </Trigger>
                    <Trigger Property="ItemsControl.HasItems" Value="False">
                        <Setter TargetName="Expander" Property="UIElement.Visibility" Value="Hidden" />
                    </Trigger>
                    <Trigger Property="TreeViewItem.IsSelected" Value="True">
                        <Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="TreeViewItem.IsSelected" Value="True" />
                            <Condition Property="Selector.IsSelectionActive" Value="False" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                        <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
                    </MultiTrigger>
                    <Trigger Property="UIElement.IsEnabled" Value="False">
                        <Setter Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

0 个答案:

没有答案