0

Good evening! I'm trying to set SetZIndex for a childe element in a XAML layout. I found information about the Panel.SetZIndex and Canvas.SetZIndex. If I'm not mistaken, it's the same thing, but a Canvas.SetZIndex is older then Panel.SetZIndex.

So, the code below is my XAML layout

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

My class, where I created an Ellipse as a child element

...
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);

...

Later I did the same with Lines:

...

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

But I want the lines to be under the Ellipses

Where did I made a mistake?

Nikita
  • 5
  • 1
  • You may want to use an ItemsControl for your shapes, e.g. as shown here: https://stackoverflow.com/a/40190793/1136211 – Clemens Jan 31 '22 at 19:09

1 Answers1

1

The Lines are actually drawn below the Ellipses.

That is however not very obvious because you are drawing both shape types with semi-transparent Brushes.

To make this clear, write for example

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

and

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

The first parameter of the Color.FromArgb method is an alpha value, i.e. an opacity. A value of 0 means fully transparent, while 255 means fully opaque.

Clemens
  • 123,504
  • 12
  • 155
  • 268