2017-05-19 145 views
0

这是一个WPF程序。我用c#代码绘制一个椭圆,然后插入到网格的子节点。 它位于TreeViewItem。为什么我在网格中绘制椭圆会被阻挡?

我只看到椭圆在前面的部分已被lengthConverter转换。

XmaI位样式:

<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}"> 
        <ControlTemplate.Resources> 
         <converters:LeftMarginMultiplierConverter Length="19" x:Key="lengthConverter" /> 
        </ControlTemplate.Resources> 
        <StackPanel> 
         <Border Name="Bd" 
           Background="{TemplateBinding Background}" 
           BorderBrush="{TemplateBinding BorderBrush}" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           Padding="{TemplateBinding Padding}"> 
          <Grid Name="Gd" Margin="{Binding Converter={StaticResource lengthConverter}, 
           RelativeSource={RelativeSource TemplatedParent}}"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="19" /> 
            <ColumnDefinition /> 
           </Grid.ColumnDefinitions> 
           <ToggleButton x:Name="Expander" 
               Style="{StaticResource ExpandCollapseToggleStyle2}" 
               IsChecked="{Binding Path=IsExpanded, 
           RelativeSource={RelativeSource TemplatedParent}}" 
               ClickMode="Press"/> 

           <ContentPresenter x:Name="PART_Header" 
                Grid.Column="1" 
                ContentSource="Header" 
                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/> 
          </Grid> 
         </Border> 
         <ItemsPresenter x:Name="ItemsHost" /> 
        </StackPanel> 
        <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="IsMouseOver" Value="True"> 
          <Setter TargetName="Bd" 
            Property="Background" 
            Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"></Setter> 
         </Trigger> 
         <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> 

C#代码:

var item = sender as TreeViewItem; 
    if (item.Items.Count > 0) return; 
    var controlTemplate = item.Template as ControlTemplate; 
    var grid=controlTemplate.FindName("Gd",item) as Grid; 
    var p = args.GetPosition((IInputElement)item); 
    var elli = new Ellipse 
    { 
     Width = 0, 
     Height = 0, 
     Fill = Brushes.White, 
     HorizontalAlignment = HorizontalAlignment.Left, 
     VerticalAlignment = VerticalAlignment.Top, 
     Opacity = .6 
    }; 
    grid.Children.Insert(0,elli); 
    var animation = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(400))); 
    animation.Completed += (s, e1) => 
    { 
     grid.Children.Remove(elli); 
    }; 
    elli.BeginAnimation(OpacityProperty, animation); 
    elli.BeginAnimation(HeightProperty, new DoubleAnimation(600, new Duration(TimeSpan.FromMilliseconds(400)))); 
    elli.BeginAnimation(WidthProperty, new DoubleAnimation(600, new Duration(TimeSpan.FromMilliseconds(400)))); 
    elli.BeginAnimation(MarginProperty, new ThicknessAnimation(new Thickness(p.X, p.Y, 0, 0), new Thickness(p.X - 300, p.Y - 300, 0, 0), new Duration(TimeSpan.FromMilliseconds(400)))); 
+1

你还没有提供足够的人来弄清楚这一点。 C#代码在一些处理程序中。哪位经理? “args”变量未定义。在XAML中,“LeftMarginMultiplierConverter”未定义。你需要简化或提供更多的答案。 – AQuirky

回答

0

因为我插入在网格的第一列中的椭圆。我设置了列来解决它。