Home > OS >  Show textbox on point A and point B after drawing a Line on Canvas WPF
Show textbox on point A and point B after drawing a Line on Canvas WPF

Time:07-21

In my project i'm drawing lines on canvas using this code.

     List<Line> DrawingLines = new List<Line>();
        DrawingLines.Add(new Line() { X1 = X(200), X2 = X(500), Y1 = Y(50), Y2 = Y(50), Stroke = Brushes.Blue });
        DrawingLines.Add(new Line() { X1 = X(500), X2 = X(600), Y1 = Y(50), Y2 = Y(100), Stroke = Brushes.Green });
        DrawingLines.Add(new Line() { X1 = X(600), X2 = X(200), Y1 = Y(100), Y2 = Y(100), Stroke = Brushes.Red });
        DrawingLines.Add(new Line() { X1 = X(200), X2 = X(200), Y1 = Y(100), Y2 = Y(50), Stroke = Brushes.Black });

        foreach (Line line in DrawingLines)
        {
            ph.Children.Add(line);
        }

What i want, but i dont know if thats possible is to have a textbox with some informations on point A(x1,y1) and also on point B(x2,y2) for every line i make.

Here is my Xaml code

<StackPanel Background="White" Width="Auto">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Canvas x:Name="ph" Height="449" Width="623" Panel.ZIndex="1" Grid.Row="0">
            <Image Height="449" Width="623" Grid.Row="0" x:Name="LogPHImage" MouseLeftButtonDown="MouseLeftButtonDown_Click" MouseMove="LogPHImage_MouseMove" Source="../UserControls/PHgraph.png" HorizontalAlignment="Center" VerticalAlignment="Top"/>
        </Canvas>
        <Border BorderThickness="1" BorderBrush="LightGray" Grid.Row="1">
            <StackPanel Orientation="Horizontal">
                <Label Width="70" Content="X Coordinates"/>
                <TextBox x:Name="xgrid" Width="70" HorizontalAlignment="Left" Background="DarkGray"/>
                <Label Width="80" HorizontalAlignment="Left">Y Coordinates</Label>
                <TextBox x:Name="ygrid" Width="70" HorizontalAlignment="Left" Background="DarkGray"/>
                <Label Width="80" HorizontalAlignment="Left" Content="Entalpy (kJ/kg)"/>
                <TextBox x:Name="entalpy" Width="70" HorizontalAlignment="Left" Background="DarkGray"/>
                <Label Width="80" HorizontalAlignment="Left" Content="Pressure bara"/>
                <TextBox x:Name="pressure" Width="70" HorizontalAlignment="Left" Background="DarkGray"/>
            </StackPanel>
        </Border>
    </Grid>

</StackPanel>

Hope you guys can help me :)

Thanks

CodePudding user response:

You can add TextBlocks at specific positions in a Canvas by setting the Canvas.Left and Canvas.Top attached properties:

foreach (Line line in DrawingLines)
{
    ph.Children.Add(line);

    var tb1 = new TextBlock { Text = "A" };
    Canvas.SetLeft(tb1, line.X1);
    Canvas.SetTop(tb1, line.Y1);
    ph.Children.Add(tb1);

    var tb2 = new TextBlock { Text = "B" };
    Canvas.SetLeft(tb2, line.X2);
    Canvas.SetTop(tb2, line.Y2);
    ph.Children.Add(tb2);
}

Add arbitrary offsets to X1, Y1, X2, Y2 for a proper alignment.

  • Related