Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不使用画布C画线的最佳方法#_C#_Wpf_Line - Fatal编程技术网

C# 不使用画布C画线的最佳方法#

C# 不使用画布C画线的最佳方法#,c#,wpf,line,C#,Wpf,Line,我想知道,不使用画布,从列表中以最大值划线的最佳方法是什么 我已经确定了最大值、最小值和中间值,我想知道不使用画布绘制直线/点的最佳方法是什么 public partial class SpectrumControl : UserControl { private double Highest; private double Minimum; private double Median; private int Total; private int Cel

我想知道,不使用画布,从列表中以最大值划线的最佳方法是什么

我已经确定了最大值、最小值和中间值,我想知道不使用画布绘制直线/点的最佳方法是什么

public partial class SpectrumControl : UserControl
{
    private double Highest;
    private double Minimum;
    private double Median;
    private int Total;
    private int CellWidth;

    public int Width { get; set; }

    public SpectrumControl()
    {

        InitializeComponent();
    }

    public void Bind(KLayer klayer)
    {
        if (Width == 0)
        {
            Width = 300;
        }

        Highest = klayer.Values.Max();
        Minimum = klayer.Values.Min();
        Median = ((Highest - Minimum) / 2) + Minimum;
        Total = klayer.Values.Count;
        CellWidth = Width / Total;
        int rowNumber = 0;
        foreach (var item in klayer.Values)
        {
            var label = CreateLabel(item, rowNumber);
            Color backgroundColour = GetColour(item);
            stk1.Children.Add(label);
            rowNumber++;
        }
    }

    private Label CreateLabel(double item, int rowNumber)
    {

        var label = new Label()
        {
            Background = new SolidColorBrush(GetColour(item)),
            Width = CellWidth
        };
        return label;

    }

    private Color GetColour(double item)
    {
        byte a = Convert.ToByte(GetTransparency(item)*255);
        Color backgroundColour;
        if (item < Median)
        {
            backgroundColour = Color.FromArgb(a, 128, 128, 255);
        }
        else if (item > Median)
        {
            backgroundColour = Color.FromArgb(a, 255, 128, 128);
        }
        else
        {
            backgroundColour = Colors.White;
        }

        return backgroundColour;
    }

    private double GetTransparency(double item)
    {
        double x = Highest - Minimum;
        double difference;
        if (item > Median)
        {
            difference = item - Median;
        }
        else
        {
            difference = Median - item;
        }

        var fraction = difference / x;
        return fraction;
    }
}
公共部分类SpectrumControl:UserControl
{
私人双最高;
私人双最小值;
私人双中位数;
私人整数合计;
私人单位宽度;
公共整数宽度{get;set;}
公共频谱控制()
{
初始化组件();
}
公共无效绑定(KLayer KLayer)
{
如果(宽度==0)
{
宽度=300;
}
最高=klayer.Values.Max();
最小值=klayer.Values.Min();
中位数=((最高-最低)/2)+最低;
总计=klayer.Values.Count;
CellWidth=宽度/总宽度;
int rowNumber=0;
foreach(klayer.Values中的变量项)
{
var label=CreateLabel(项目,行号);
颜色背景颜色=GetColor(项目);
stk1.儿童.添加(标签);
行数++;
}
}
专用标签CreateLabel(双项目,整数行编号)
{
var label=新标签()
{
背景=新的SolidColorBrush(GetColor(项目)),
宽度=单元格宽度
};
退货标签;
}
私人颜色GetColor(双色)
{
字节a=转换为字节(透明度(项目)*255);
颜色背景颜色;
如果(项目<中位数)
{
backgroundcolor=Color.FromArgb(a,128,128,255);
}
否则,如果(项目>中值)
{
backgroundcolor=Color.FromArgb(a,255,128,128);
}
其他的
{
背景颜色=颜色。白色;
}
返回背景色;
}
专用双透明(双项目)
{
双x=最高-最低;
双重差异;
如果(项目>中值)
{
差异=项目-中位数;
}
其他的
{
差异=中位数-项目;
}
var分数=差值/x;
收益率;
}
}

好吧,假设您要使用类似于
GridPanel
或任何其他面板的东西,实际上,您可以这样做:

var line = new Line();
line.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
line.X1 = 1;
line.X2 = 50;
line.Y1 = 1;
line.Y2 = 50;
line.HorizontalAlignment = HorizontalAlignment.Left;
line.VerticalAlignment = VerticalAlignment.Center;
line.StrokeThickness = 2;
grid.Children.Add(line);
同样的事情也可以在XAML中实现,但看起来您更喜欢在代码隐藏中工作,所以这就是我在这里发布的内容

参考:


我不知道你为什么要避免画布,虽然(嗯,为什么有人告诉你这样做)。我已经用画布创建了很多绘图。

有没有什么原因让你不能使用画布?刚才有人告诉我暂时不要使用画布,我做了研究,每个人都在使用画布,这是可能的还是我会更好地使用画布?谢谢,这正是我所需要的。我自己也不是100%,为什么他们告诉我不要在XAML中这么做