The entire row selection effect of WPF Treeview

Source: Internet
Author: User

WPF native Treeview can only select contentpresenter

 

Effect

The main point is that

1. The template structure of treeviewitem is modified to a uniform background color and fill length.

2. the background color is not filled enough due to the right offset of the child node,

This is because the itemspresenter in the template is in the second column of the layout. Because the initial position of the item contained in the item is in the second column of the top level, the width of the first column is offset at a time.

Solution:

Baidu once found a post about how to implement it. The main principle is to remove the indentation between layers in the layout so that the background color of each level can be filled to the leftmost, then, the Code records the item level and calculates the offset and offset items. But I think this is too troublesome, so I wrote a clever pure XAML method. Code on

<Style TargetType="{x:Type TreeViewItem}">            <Setter Property="Foreground" Value="#FFF0F0F0"/>            <Setter Property="Template">                <Setter.Value>                    <ControlTemplate TargetType="{x:Type TreeViewItem}">                        <Grid>                            <Grid.ColumnDefinitions>                                <ColumnDefinition  Width="16"/>                                <ColumnDefinition/>                            </Grid.ColumnDefinitions>                            <Grid.RowDefinitions>                                <RowDefinition/>                                <RowDefinition/>                            </Grid.RowDefinitions>                            <Border Grid.ColumnSpan="2" Margin="-1600,0,0,0"  x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"  SnapsToDevicePixels="true"/>                            <ToggleButton  x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>                            <ContentPresenter Grid.Column="1" x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>                             <ItemsPresenter  x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>                                                 </Grid>                        <ControlTemplate.Triggers>                            <Trigger Property="IsExpanded" Value="false">                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>                            </Trigger>                            <Trigger Property="HasItems" Value="false">                                <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>                            </Trigger>                            <Trigger Property="IsSelected" Value="true">                                <Setter Property="Background" TargetName="Bd" Value="#FF404040" />                                                              <Setter Property="Background" TargetName="Expander" Value="#FF404040"/>                            </Trigger>                            <MultiTrigger>                                <MultiTrigger.Conditions>                                    <Condition Property="IsSelected" Value="true"/>                                    <Condition Property="IsSelectionActive" Value="false"/>                                </MultiTrigger.Conditions>                                <Setter Property="Background" TargetName="Bd" Value="#FF404040"/>                                                              <Setter Property="Background" TargetName="Expander" Value="#FF404040"/>                            </MultiTrigger>                            <Trigger Property="IsEnabled" Value="false">                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>                            </Trigger>                        </ControlTemplate.Triggers>                    </ControlTemplate>                </Setter.Value>            </Setter>        </Style>

It is simple and practical, but it still needs to be established on the basis of figuring out the layout. Of course, if the length of the parent-child relationship exceeds 100 (16*100), the problem will arise, however, generally it will not be as long as 100 .. =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.