none
Treeview 当设置鼠标移上去子节点变色时,他会把父节点一起变色了。 RRS feed

  • 问题

  • 各位好:

             当我修改treeview的样式,添加IsMouseOver时,不只是子节点变色了,他的父节点的文字颜色也发生了变化。请问应该怎样才能只修改子节点的颜色

    <Window x:Class="下拉显示三个控件.Window3"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:下拉显示三个控件"
            mc:Ignorable="d"
            Title="Window3" Height="300" Width="300">
        <Window.Resources>
            <local:TreeViewLineConverter x:Key="LineConverter"/>
            <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
                <Setter Property="Focusable" Value="False"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ToggleButton">
    
                            <Grid Width="15" Height="13" SnapsToDevicePixels="True">
                                <!-- Rectangle 9x9 pixels -->
                                <Rectangle Width="9" Height="9" 
              Stroke="#919191" SnapsToDevicePixels="true">
                                    <Rectangle.Fill>
                                        <LinearGradientBrush EndPoint="0.5,2" StartPoint="0.5,0">
                                            <GradientStop Color="White" Offset="0"/>
                                            <GradientStop Color="Silver" Offset="0.5"/>
                                            <GradientStop Color="LightGray" Offset="1"/>
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                </Rectangle>
                                <!-- Vertical line inside rectangle -->
                                <Rectangle x:Name="ExpandPath" Width="1" 
              Height="5" Stroke="Black" SnapsToDevicePixels="true"/>
                                <!-- Horizontal line inside rectangle -->
                                <Rectangle Width="5" Height="1" 
              Stroke="Black" SnapsToDevicePixels="true"/>
                            </Grid>
    
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Visibility"  
                TargetName="ExpandPath" Value="Collapsed"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="TreeViewItemFocusVisual">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border>
                                <Rectangle Margin="0,0,0,0"
                         StrokeThickness="5"
                         Stroke="Black"
                         StrokeDashArray="1 1"
                         Opacity="0"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalContentAlignment"
              Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="VerticalContentAlignment"
              Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Padding" Value="1,0,0,0"/>
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TreeViewItem}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="19" Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
    
                                <!-- Connecting Lines -->
                                <Rectangle x:Name="HorLn" Height="1" Stroke="#8888"  Margin="9,0,0,0" SnapsToDevicePixels="true" StrokeDashArray="2" StrokeThickness="1"/>
                                <Rectangle x:Name="VerLn" Width="1" Stroke="#8888" Grid.RowSpan="2" SnapsToDevicePixels="true" StrokeDashArray="2" StrokeThickness="1"/>
                                <ToggleButton x:Name="Expander"
                          Style="{StaticResource ExpandCollapseToggleStyle}"
                          IsChecked="{Binding Path=IsExpanded,
                          RelativeSource={RelativeSource TemplatedParent}}"
                          ClickMode="Press"/>
                                <Border Name="Bd"
                      Grid.Column="1"
                      Background="{TemplateBinding Background}"
                      BorderBrush="{TemplateBinding BorderBrush}"
                      BorderThickness="{TemplateBinding BorderThickness}"
                      Padding="{TemplateBinding Padding}">
                                    <ContentPresenter x:Name="PART_Header"
                              ContentSource="Header"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                </Border>
                                <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
                            </Grid>
                            <ControlTemplate.Triggers>
    
                                <!-- This trigger changes the connecting lines if the item is the last in the list -->
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource LineConverter}}" Value="true">
                                    <Setter TargetName="VerLn" Property="Height" Value="8"/>
                                    <Setter TargetName="VerLn" Property="VerticalAlignment" Value="Top"/>
                                </DataTrigger>
                                <Trigger Property="IsExpanded" Value="false">
                                    <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                                </Trigger>
                                <Trigger Property="HasItems" Value="false">
                                    <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="HasHeader" Value="false"/>
                                        <Condition Property="Width" Value="Auto"/>
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="HasHeader" Value="false"/>
                                        <Condition Property="Height" Value="Auto"/>
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                                </MultiTrigger>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter Property="Foreground" Value="#3F91D7"/>
                                </Trigger>
    
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            
        </Window.Resources>
        <Grid>
            <TreeView >
                <TreeViewItem Header="亚洲">
                    <TreeViewItem Header="中国">
                        <TreeViewItem Header="北京"/>
                        <TreeViewItem Header="天津"/>
                        <TreeViewItem Header="重庆"/>
                        <TreeViewItem Header="成都"/>
                    </TreeViewItem>
                    <TreeViewItem Header="越南">
                        <TreeViewItem Header="芽庄"/>
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem Header="非洲">
                    <TreeViewItem Header="南非">
                        <TreeViewItem Header="坎贝拉"/>
                        <TreeViewItem Header="天津"/>
                        <TreeViewItem Header="重庆"/>
                        <TreeViewItem Header="成都"/>
                    </TreeViewItem>
                    <TreeViewItem Header="冈比亚">
                        <TreeViewItem Header="芽庄"/>
                    </TreeViewItem>
                </TreeViewItem>
            </TreeView>
        </Grid>
    </Window>
    

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace 下拉显示三个控件
    {
        /// <summary>
        /// Window3.xaml 的交互逻辑
        /// </summary>
        public partial class Window3 : Window
        {
            public Window3()
            {
                InitializeComponent();
            }
        }
    
         class TreeViewLineConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                TreeViewItem item = (TreeViewItem)value;
                ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
                return ic.ItemContainerGenerator.IndexFromContainer(item) == ic.Items.Count - 1;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }
    }
    

    2017年8月6日 6:20

答案


  • Hi 彭123,

    你可以尝试下面的代码来设置鼠标移上去子节点变色时,他不会把父节点一起变色。

     <TreeView >
                <TreeView.Resources>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Border>
                                        <TextBlock Text="{Binding}"/>
                                        <Border.Style>
                                            <Style TargetType="Border">
                                                <Setter Property="BorderThickness" Value="1"/>
                                                <Setter Property="BorderBrush" Value="Transparent"/>
                                                <Style.Triggers>
                                                    <MultiDataTrigger>
                                                        <MultiDataTrigger.Conditions>
                                                            <Condition Binding="{Binding RelativeSource={RelativeSource Self},
                                                      Path=IsMouseOver}" Value="True"/>
                                                            <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem},
                                                      Path=IsSelected}" Value="False"/>
                                                        </MultiDataTrigger.Conditions>
                                                        <Setter Property="Background" Value="BlueViolet"/>
                                                        <Setter Property="BorderBrush" Value="#ffa5d7f0"/>
                                                    </MultiDataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Border.Style>
                                    </Border>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeView.Resources>
                <TreeViewItem Header="亚洲">
                    <TreeViewItem Header="中国">
                        <TreeViewItem Header="北京"/>
                        <TreeViewItem Header="天津"/>
                        <TreeViewItem Header="重庆"/>
                        <TreeViewItem Header="成都"/>
                    </TreeViewItem>
                    <TreeViewItem Header="越南">
                        <TreeViewItem Header="芽庄"/>
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem Header="非洲">
                    <TreeViewItem Header="南非">
                        <TreeViewItem Header="坎贝拉"/>
                        <TreeViewItem Header="天津"/>
                        <TreeViewItem Header="重庆"/>
                        <TreeViewItem Header="成都"/>
                    </TreeViewItem>
                    <TreeViewItem Header="冈比亚">
                        <TreeViewItem Header="芽庄"/>
                    </TreeViewItem>
                </TreeViewItem>
            </TreeView>
    


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 彭123 2017年8月8日 1:22
    2017年8月7日 6:53
    版主

全部回复


  • Hi 彭123,

    你可以尝试下面的代码来设置鼠标移上去子节点变色时,他不会把父节点一起变色。

     <TreeView >
                <TreeView.Resources>
                    <Style TargetType="TreeViewItem">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <Border>
                                        <TextBlock Text="{Binding}"/>
                                        <Border.Style>
                                            <Style TargetType="Border">
                                                <Setter Property="BorderThickness" Value="1"/>
                                                <Setter Property="BorderBrush" Value="Transparent"/>
                                                <Style.Triggers>
                                                    <MultiDataTrigger>
                                                        <MultiDataTrigger.Conditions>
                                                            <Condition Binding="{Binding RelativeSource={RelativeSource Self},
                                                      Path=IsMouseOver}" Value="True"/>
                                                            <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem},
                                                      Path=IsSelected}" Value="False"/>
                                                        </MultiDataTrigger.Conditions>
                                                        <Setter Property="Background" Value="BlueViolet"/>
                                                        <Setter Property="BorderBrush" Value="#ffa5d7f0"/>
                                                    </MultiDataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Border.Style>
                                    </Border>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeView.Resources>
                <TreeViewItem Header="亚洲">
                    <TreeViewItem Header="中国">
                        <TreeViewItem Header="北京"/>
                        <TreeViewItem Header="天津"/>
                        <TreeViewItem Header="重庆"/>
                        <TreeViewItem Header="成都"/>
                    </TreeViewItem>
                    <TreeViewItem Header="越南">
                        <TreeViewItem Header="芽庄"/>
                    </TreeViewItem>
                </TreeViewItem>
                <TreeViewItem Header="非洲">
                    <TreeViewItem Header="南非">
                        <TreeViewItem Header="坎贝拉"/>
                        <TreeViewItem Header="天津"/>
                        <TreeViewItem Header="重庆"/>
                        <TreeViewItem Header="成都"/>
                    </TreeViewItem>
                    <TreeViewItem Header="冈比亚">
                        <TreeViewItem Header="芽庄"/>
                    </TreeViewItem>
                </TreeViewItem>
            </TreeView>
    


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 彭123 2017年8月8日 1:22
    2017年8月7日 6:53
    版主
  • 楼上的答案完美的解决了问题。
    2017年8月8日 1:23