我根据自己的需要调整了TreeViewItemStyle,当我像这样硬编码我的TreeViewItems时,它的效果非常好:
<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>
(我无法发布图片,所以我必须链接到它们)
第一张图片很完美,但第二张图片是在我尝试数据绑定TreeView时创建的。对于Hierachical Data来说,下面的代码可以正常工作,但它的显示方式与我想要的不同。
<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>
你可以看到它产生的令人讨厌的效果。
出了什么问题?
我的风格如下(为了方便阅读,删除了一些画笔和动画):
<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>
答案 0 :(得分:0)
从图像的外观来看,这可能是项目容器的问题。将集合绑定到某些ItemsControl时,不应将容器项放在DataTemplate中,ItemsControl会将DataTemplate包装在适当的容器本身中(在本例中为TreeViewItem)。 因此,由于您的DataTemplate中有另一个容器,因此最终会有两个