数据绑定时自定义 WPF TreeViewItem 样式行为不正确
我已经根据我的需要调整了 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 进行数据绑定时创建的。就分层数据而言,下面的代码工作正常,但它的显示方式与我想要的不同。
<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>
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)
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从图像的外观来看,这可能是项目容器的问题。当您将集合绑定到某些 ItemsControl 时,您不应将容器项放入 DataTemplate 中,ItemsControl 会将您的 DataTemplate 包装在适当的容器本身中(在本例中为 TreeViewItem)。
因此,由于您的 DataTemplate 中有另一个容器,因此您最终会得到两个,
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,