自定义 WPF TreeViewItem 样式在数据绑定时行为不正确 [英] Custom WPF TreeViewItem style is behaving incorrectly when databound

查看:29
本文介绍了自定义 WPF TreeViewItem 样式在数据绑定时行为不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据自己的需要调整了 TreeViewItemStyle,当我像这样对 TreeViewItems 进行硬编码时它工作得很好:

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)

差异图片

第一个图像是完美的,但是当我尝试对我的 TreeView 进行数据绑定时创建了第二个图像.就分层数据而言,下面的代码工作正常,但它的显示方式与我想要的不同.

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.

出了什么问题?

我的风格如下(为了便于阅读,删除了一些画笔和动画):

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>

推荐答案

从图片上看,这可能是项目容器的问题.当您将集合绑定到某些 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,

这篇关于自定义 WPF TreeViewItem 样式在数据绑定时行为不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆