如下图所示:【TreeView节点被选中并且有焦点时的样式】
【ContextMenu弹出后节点失去焦点时的样式】
除了改TreeViewItem模板样式之外,有没有其它简单的方法可以解决这种情况?如果定义TreeViewItem模板的话,怎么定义?谢谢。

解决方案 »

  1.   


    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
        this.treeView1.SelectedNode = ((sender as TreeView).GetNodeAt(e.Location)) as TreeNode;
    }
      

  2.   

    楼上有蹭分嫌疑啊。你的代码在WPF中肯定是不能用的。
      

  3.   

    http://blog.csdn.net/lhx527099095/article/details/7999116http://blog.csdn.net/lhx527099095/article/details/7999005对着里面的修改就是了 框架是有了你自己改下样式就ok
      

  4.   

    没注意是WPF。还以为是C#......
      

  5.   

    焦点的来源是鼠标, 你都把鼠标给了 快捷菜单了, 你还想叫 TreeNode也有 焦点这个 不呵护常理吧,你可以做 虚拟的,给样式吗 
      

  6.   

    http://msdn.microsoft.com/en-us/library/ms788727(v=vs.85).aspx从上面的链接可以找到默认的treeviewitem template
    然后复制到你的project作为style加载然后去掉
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsSelected" Value="true"/>
            <Condition Property="IsSelectionActive" Value="false"/>
        </MultiTrigger.Conditions>
        <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    </MultiTrigger>这个trigger就是想表达当选择但不激活时设置background为controlBrushKey,去掉就能满足你的要求
    May it helps.
      

  7.   


    完成了,非常感谢。修改过之后的完整模板代码如下:<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    <!--=================================================================
          TreeViewItem
      ==================================================================-->
        <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid
              Width="15"
              Height="13"
              Background="Transparent">
                            <Path x:Name="ExpandPath"
                HorizontalAlignment="Left" 
                VerticalAlignment="Center" 
                Margin="1,1,1,1"
                Fill="#444"
                Data="M 4 0 L 8 4 L 4 8 Z"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked"
                   Value="True">
                                <Setter Property="Data"
                    TargetName="ExpandPath"
                    Value="M 0 4 L 8 4 L 4 8 Z"/>
                            </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 2"
                     Opacity="0"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="{x:Type TreeViewItem}"
         TargetType="{x:Type TreeViewItem}">
            <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>
                            <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>
                            <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="IsSelected"
                   Value="true">
                                <Setter TargetName="Bd"
                    Property="Background"
                    Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                            </Trigger>
                            <!--<MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected"
                         Value="true"/>
                                    <Condition Property="IsSelectionActive"
                         Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd"
                    Property="Background"
                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                            </MultiTrigger>-->
                            <Trigger Property="IsEnabled"
                   Value="false">
                                <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    顺便感谢楼上的几位。结贴了。