如何使多行内容显示在画布上

时间:2019-07-18 17:03:03

标签: c# wpf xaml

基本上,我正在尝试根据用户输入在画布上动态显示行数。我创建了一种画线的方法,但是我需要多个才能同时显示在屏幕上。

我需要的行数来自pload变量,xcoord / pload将确定每行之间应放置多少空间,因为我需要它们沿xcoord区域均匀分布。

我尝试了一些if语句的变体,但这里缺少一些关键概念。

public FEModel()
    {

        InitializeComponent();
        DataContext = this;
        AddLine();

    }   
private double xval;
public double xcoord
    {
        get => xval;
        set
        {
            xval = value;
            CreateARectangle();
        }
    }
private double pval;

    public double pload
    {
        get => pval;
        set
        {
            pval = value;

        }
    }    

//holds amount of space that should be between each line
private double nodeNum =0;

    public void AddLine()
    {
        Line nodeLine = new Line();
        nodeLine.X1 = 1;
            nodeLine.X2 = 30;
            nodeLine.Y1 = 1;
            nodeLine.Y2 = 30;
            nodeLine.Stroke = Brushes.LightSteelBlue;
            nodeLine.StrokeThickness = 4;

            nodeNum = (xcoord / pload);



        while (nodeLine.X1 < xcoord)
        {
            //keep adding lines until it reaches end of xcoord area, increasing by the amount of nodeNum.
            main.Children.Add(nodeLine);
            nodeLine.X1 += nodeNum;
            nodeLine.X2 += nodeNum;

        }
    }

这里的XAML仅显示xcoord和pload编号来自何处:

<TextBox Text="{Binding xcoord, UpdateSourceTrigger=PropertyChanged}" Name="x" Height="20" Width="40" Grid.Row="1" Grid.Column="2" />
<TextBox Text="{Binding pload, UpdateSourceTrigger=PropertyChanged}"  Name="p" Grid.Row="1" Height="20" Width="40" Grid.Column="2"></TextBox>

预期结果是屏幕上显示的行以nodeNum可变量隔开。

0 个答案:

没有答案