之前在写游戏场景树的时候遇到的几个细节问题,颠覆了我以往对标准控件的了解。 问题一: 我们都知道树形控件看起来是分级的,有父子关系。平时在点击右键的时候,它不会自动选中鼠标下的子节点,也没有提供类似Winform中的HitTest属性。这需要我们自己另辟他法实现。

/// <summary>
/// 鼠标右键按下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Event_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    TreeViewItem item = GetTemplatedAncestor<TreeViewItem>(e.OriginalSource as FrameworkElement);
    if (item != null)
    {
        // ...
    }
}

/// <summary>
/// 获取鼠标指针下的节点
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="element"></param>
/// <returns></returns>
private T GetTemplatedAncestor<T>(FrameworkElement element) where T : FrameworkElement
{
    if (element is T)
    {
        return element as T;
    }

    FrameworkElement templatedParent = element.TemplatedParent as FrameworkElement;
    if (templatedParent != null)
    {
        return GetTemplatedAncestor<T>(templatedParent);
    }

    return null;
}

问题二:
当给WPF中的TreeView设置一个ContextMenu时,会发现菜单弹出之后,TreeView将失去焦点,TreeView中的节点也失去了焦点。虽然节点依然处于选中状态,但选中的背景颜色变成了灰色,用户体验可能不太好。目前我还没有找到一种比较完美的解决方法,只能通过修改XAML模板实现对TreeViewItem的背景颜色进行控制。

<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>