为什么函数 Panel.SetZIndex() 在我的代码中不起作用?

Why does the functional Panel.SetZIndex() not work in my code?

晚上好!我正在尝试为 XAML 布局中的子元素设置 SetZIndex。我找到了有关 Panel.SetZIndex 和 Canvas.SetZIndex 的信息。如果我没记错的话,这是一回事,但是 Canvas.SetZIndex 比 Panel.SetZIndex.

所以,下面的代码是我的 XAML 布局

<Window x:Class="MLvisualisator.MainWindow"
        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:MLvisualisator"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="50px"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
            <TextBox Name="CCountOfNeyrons" Width="200" Margin="10" BorderBrush="Aqua"></TextBox>
            <TextBox Name="CountOfColum" Width="200" Margin="10" BorderBrush="Aqua"></TextBox>
            <Button Name="Generate" Content="Generate" Click="GenerateFun"/>
        </StackPanel>
        <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Grid.Column="0" Grid.Row="1">
            <Canvas Name="TestAdd">
                //there will be child elements
            </Canvas>
        </ScrollViewer>
    </Grid>
</Window>

我的 class,我在其中创建了一个椭圆作为子元素

...
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(28, 28, 28, 0);

Ellipse ell = new Ellipse
{
    Width = sc.NeuronRadius,
    Height = sc.NeuronRadius,
    Fill = mySolidColorBrush,
    Name = id
};

Canvas.SetLeft(ell, columCanvas);
Canvas.SetTop(ell, rowCanvas);

Panel.SetZIndex(ell, 2); // It doesn't work, and I don't know why

TestAdd.Children.Add(ell);

...

后来我对线条做了同样的事情:

...

SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(120, 255, 0, 0);

Line line = new Line // x1, x2, y1, y2, sc.NeuronRadius are an integer params
{
   X1 = x1 + (sc.NeuronRadius / 2),
   X2 = x2 + (sc.NeuronRadius / 2),
   Y1 = y1 + (sc.NeuronRadius / 2),
   Y2 = y2 + (sc.NeuronRadius / 2),
   Name = "W" + start + "_" + end,
   StrokeThickness = 4,
   Stroke = mySolidColorBrush,
};

TestAdd.Children.Add(line);
Panel.SetZIndex(line, 0); // It doesn't work, and I don't know why
...

The result is this picture

但我希望线条位于省略号下方

我哪里弄错了?

线条实际上画在椭圆下方。

但这不是很明显,因为您正在使用 semi-transparent 笔刷绘制两种形状类型。

为了清楚起见,写成例子

Ellipse ell = new Ellipse
{
    Fill = Brushes.LightGray,
    ...
};

Line line = new Line
{
    Stroke = Brushes.Red,
    ...
};

Color.FromArgb 方法的第一个参数是 alpha 值,即不透明度。值 0 表示完全透明,而 255 表示完全不透明。