Custom WPF TreeViewItem style is behaving incorrec

2019-09-06 07:45发布

I have adapted a TreeViewItemStyle to my need and it works great when I have hardcoded my TreeViewItems like so:

<TreeView Grid.Column="0" 
              HorizontalAlignment="Stretch" 
              Name="treeView1" 
              VerticalAlignment="Stretch" >
        <TreeView.Items>
            <TreeViewItem Header="McDonalds" IsExpanded="True">
                <TreeViewItem.Items>
                    <TreeViewItem Header="Burger" IsExpanded="True" IsSelected="True">
                        <TreeViewItem.Items>
                            <TreeViewItem Header="Meat" />
                        </TreeViewItem.Items>
                    </TreeViewItem>
                </TreeViewItem.Items>
            </TreeViewItem>
            <TreeViewItem Header="KFC" >
                <TreeViewItem.Items>
                    <TreeViewItem Header="Chicken"/>
                </TreeViewItem.Items>
            </TreeViewItem>
            <TreeViewItem Header="Hungry Jacks">
                <TreeViewItem.Items>
                    <TreeViewItem Header="Onion Rings" />
                </TreeViewItem.Items>
            </TreeViewItem>
        </TreeView.Items>
    </TreeView>

(I cannot post images, so I must link to them)

Images of Differences

First image is perfect, but the second is created when I try to databind my TreeView. The code below works fine as far as Hierachical Data goes, but it displays differently than I want it to.

<TreeView DataContext="{Binding TopLevelCategories}">
<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type model:Category}" ItemsSource="{Binding Path=Category1}">
        <TreeViewItem Header="{Binding Path=Name}" Tag="{Binding}" />
    </HierarchicalDataTemplate>
</TreeView.Resources>

You can see the yucky effect that it creates.

What is going wrong?

My style is below (some brushes and animations removed for reading ease):

    <Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
    <Setter Property="Foreground" Value="#FFD0EBFF" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <Border x:Name="border1" Padding="1,1,0,0">
                    <Grid Width="Auto" Height="Auto">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition MinWidth="19" Width="Auto" />
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.5*"/>
                            <RowDefinition Height="0.5*"/>
                            <RowDefinition Height="0.5*" />
                        </Grid.RowDefinitions>
                        <ToggleButton x:Name="Expander"
                                      Style="{StaticResource ExpandCollapseToggleStyle}"
                                      IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
                                      ClickMode="Press"
                                      Grid.RowSpan="2"/>
                        <Border Grid.Column="1" Grid.RowSpan="2" Background="#FF062B63" CornerRadius="0,0,0,0" x:Name="border1_0" Padding="1" ></Border>
                        <Border Width="Auto" Height="Auto" Grid.Column="1" Grid.RowSpan="2" Margin="1" CornerRadius="0,0,0,0" x:Name="border1_1" Background="{DynamicResource headerNormalGradient}">
                        </Border>
                        <Border SnapsToDevicePixels="True" Grid.Column="1" Grid.Row="0" Margin="1" CornerRadius="0,0,0,0" x:Name="border1_2" Background="{DynamicResource headerNormalGlare}">
                        </Border>
                        <Rectangle x:Name="leftGlare" HorizontalAlignment="Left" Margin="1" Width="1" Stroke="{x:Null}" StrokeThickness="0" Grid.Column="1" Grid.RowSpan="2">
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
                                    <GradientStop Color="#00C0DAFB" Offset="0"/>
                                    <GradientStop Color="#57CDE2FF" Offset="1"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <Border Grid.Column="1" Grid.RowSpan="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                            <ContentPresenter Margin="4,3,4,5" ContentSource="Header"/>
                        </Border>
                        <Rectangle x:Name="WhiteRectangle"
                                   HorizontalAlignment="Stretch" 
                                   VerticalAlignment="Stretch" 
                                   Grid.RowSpan="2"
                                   Grid.Column="1" 
                                   Fill="{StaticResource Arrow58}"
                                   Margin="0,0,-1,0" 
                                   Visibility="Hidden"/>
                        <Rectangle x:Name="BlackRectangle" 
                                   HorizontalAlignment="Stretch" 
                                   VerticalAlignment="Stretch" 
                                   Grid.RowSpan="2"
                                   Grid.Column="1" 
                                   Fill="{StaticResource Arrow57}"
                                   Margin="0,0,0,0"
                                   Visibility="Hidden"/>
                        <ItemsPresenter x:Name="ChildItems" Grid.Column="1" Grid.Row="2"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded"
                                 Value="false">
                        <Setter TargetName="ChildItems"
                                    Property="Visibility"
                                    Value="Collapsed"/>
                    </Trigger>
                    <Trigger Property="HasItems"
                                 Value="false">
                        <Setter TargetName="Expander"
                                    Property="Visibility"
                                    Value="Hidden"/>
                    </Trigger>
                    <Trigger Property="IsSelected"
                                 Value="true">
                        <Setter TargetName="WhiteRectangle"
                                    Property="Visibility"
                                    Value="Visible" />
                        <Setter TargetName="BlackRectangle"
                                    Property="Visibility"
                                    Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1条回答
一纸荒年 Trace。
2楼-- · 2019-09-06 08:49

From the looks of the image this might be a problem with the item container. When you bind a collection to some ItemsControl you should not put the container item inside the DataTemplate, the ItemsControl will wrap your DataTemplate in an appropriate container itself (in this case a TreeViewItem).
So since your DataTemplate has another container in it you end up with two,

查看更多
登录 后发表回答