简体   繁体   English

在 Canvas WPF 上绘制一条线后,在 A 点和 B 点上显示文本框

[英]Show textbox on point A and point B after drawing a Line on Canvas WPF

In my project i'm drawing lines on canvas using this code.在我的项目中,我正在使用此代码在 canvas 上画线。

     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.我想要什么,但我不知道是否可能有一个文本框,其中包含关于点 A(x1,y1) 以及点 B(x2,y2) 的一些信息,用于我制作的每一行。

Here is my Xaml code这是我的 Xaml 代码

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

You can add TextBlocks at specific positions in a Canvas by setting the Canvas.Left and Canvas.Top attached properties:您可以通过设置Canvas.LeftCanvas.Top附加属性在 Canvas 中的特定位置添加 TextBlock:

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.为 X1、Y1、X2、Y2 添加任意偏移以进行正确对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM