开发者

wpf - can't display TreeView items

I have the following code which supposed to display a TreeView (the original code is more complicated, thus the complex control hirerchy):

<TreeView ItemsSource="{Binding TreeRoot}"
      HorizontalAlignment="Left">


    <TreeView.Resources>

        <DataTemplate x:Key="CTemplate">
            <Border >

                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="auto"/>
                    </Grid.ColumnDefinitions>

                    <ToggleButton 
                            Grid.Column="1"
            x:Name="Expander"
                            HorizontalAlignment="Right"
                            ClickMode="Press"/>                                          


                    <TextBlock  Text="{Binding Name}" 
                Grid.Column="0" />
                </Grid>

            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=HasItems, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="False">
                    <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </DataTemplate.Triggers>

        </DataTemplate>



        <Style TargetType="{x:Type TreeViewItem}" x:Key="aaa">
            <Setter Property="ItemsSource" Value="{Binding Children}"/>
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>                


            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <StackPanel>
                            <Border Name="Bd"
                                        Background="{TemplateBinding Background}">

                            </Border>
                            <ItemsPresenter x:Name="ItemsP" />
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="false">
                                <Setter TargetName="ItemsP"
                                        Property="Visibility"
                                        Value="Collapsed"/>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>

        <Style TargetType="{x:Type TreeView}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeView}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}">
                             <DockPanel>
                                    <ItemsPresenter/>
                                </DockPanel>

                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemContainerStyle" Value="{StaticResource aaa}" />
    开发者_如何学C        <Setter Property="ItemTemplate" Value="{CTemplate}" />
        </Style>
    </TreeView.Resources>
</TreeView>

Could you please help me figure out why my items are not displayed? (The page appears empty)

Thanks, Li


You don't have anything showing the Header of the TreeViewItem:

<Style TargetType="{x:Type TreeViewItem}" x:Key="aaa">
    ...
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <StackPanel>
                    <Border Name="Bd" Background="{TemplateBinding Background}">
                        <ContentPresenter x:Name="PART_Header"
                                          ContentSource="Header"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ItemsPresenter x:Name="ItemsP" />
                </StackPanel>
                ...
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Above I added the ContentPresenter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜