为什么我在网格中绘制椭圆会被阻挡?

时间:2017-05-19 01:13:08

标签: wpf

这是一个WPF计划。 我用c#代码绘制一个椭圆,然后插入网格的Children。 它位于TreeViewItem。

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

xp风格:

<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 个答案:

答案 0 :(得分:0)

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