简体   繁体   English

对数据网格中的列进行自定义评估,并将Observable Collection绑定到DataGrid的列

[英]custom evaluation of columns in data grid and Binding of Observable Collection to a Column of DataGrid

Situation : In general: I have a disc and want to measure the thickness. 状况 :通常:我有一个光盘,想要测量厚度。 The measuring system are two sensors which have a defined distance from each other. 测量系统是两个传感器,彼此之间具有确定的距离。 The disc is than placed between these sensors which then measure their respective distance to the disc. 然后将光盘放置在这些传感器之间,然后再测量它们与光盘的距离。

Precisely : The thickness is evaluated as an average over several values. 精确地 :厚度被评估为多个值的平均值。 So I measure the thickness at a specific radius and than rotate the disc and take the average over the whole rotation as mean value for that specific radius. 因此,我测量特定半径处的厚度,然后旋转圆盘,并将整个旋转过程中的平均值作为该特定半径的平均值。 Then the total average is taken over 5 such radius measurements. 然后,对5个这样的半径测量值取总平均值。

Requirements : There should be a table which looks like this: 要求 :应该有一个看起来像这样的表:

| radius | mean thickness | deviation|
--------------------------------------
|  5cm   |    700µm       |    10µm  |
|  7cm   |    702µm       |    11µm  |
|  9cm   |    695µm       |    17µm  |
| 11cm   |    699µm       |     9µm  |
| 12cm   |    703µm       |    12µm  |
--------------------------------------
mean:         700µm            10µm

General Question : How can I achieve that? 一般问题 :如何实现?

What I have done : I created a Class namend Disc which looks like this: 我做了什么 :我创建了一个名为name的Disc ,看起来像这样:

public class Disc
{
    private ObservableCollection<double> measuredRadii;
    public ObservableCollection<double> MeasuredRadii { get { return measuredRadii; } }

    private ObservableCollection<DistanceMeasurementsResults> measurementValues;
    public ObservableCollection<DistanceMeasurementsResults> MeasurementValues { get { return measurementValues; } }

    public Disc()
    {
        // these radii are read from settings file
        measuredRadii = new ObservableCollection<double>() { 5, 7, 9, 11, 12 };
        measurementValues = new ObservableCollection<DistanceMeasurementsResults>();
    }

    public void AddMeasurementsResults(DistanceMeasurementsResults results)
    {
        measurementValues.Add(results);
    }
}

And as you can see, there is a DistanceMeasurementsResults class: 如您所见,有一个DistanceMeasurementsResults类:

public class DistanceMeasurementsResults
{
    public Point Position;
    public double DistanceFromSensor1;
    public double DistanceFromSensor2;
}

The Position has as X the radius and as Y the angle phi. Position具有与X半径和作为Y角度披。

To realize the table I tried to use a DataGrid in my MainWindow.xaml. 为了实现该表,我尝试在MainWindow.xaml中使用DataGrid。 I thought I would fill the radii which are in disc.MeasuredRadii in to the first column, and then calculate the average thickness for each row with a neat little function like the following: let's assume I have the current row index as rowIndex . 我以为我可以将disc.MeasuredRadii中的半径填充到第一列中,然后使用如下所示的简洁小函数计算每一行的平均厚度:假设我将当前行索引作为rowIndex Then the average thickness for that row in column "mean thickness" would be calculated like: 然后,在“平均厚度”列中该行的平均厚度将计算如下:

var measurementValuesWithCurrentRadius = disc.MeasurementValues.Where(x => x.Position.X == disc.MeasuredRadii[rowIndex]);
var measuredThicknesses = measurementValuesWithCurrentRadius.Select(x => (DISTANCE_BETWEEN_SENSORS - x.DistanceFromSensor1 - x.DistanceFromSensor2));
double measuredThicknesses = measuredDistances.Average();

I hoped I could eventually bind this method to the second column (that with Header "mean thickness"). 我希望我最终可以将此方法绑定到第二列(标题为“平均厚度”的列)。

The relevant MainWindow.xaml looks like this: 相关的MainWindow.xaml如下所示:

<DataGrid x:Name="MeasureDataGrid" IsReadOnly="True" AutoGenerateColumns="False">
</DataGrid>

And then I tried to fill the DataGrid in the code-behind (MainWindow.xaml.cs). 然后,我尝试将DataGrid填充在背后的代码(MainWindow.xaml.cs)中。 At first I just wanted to see the radii in the DataGrid, but I failed already at this point... 起初,我只是想查看DataGrid中的半径,但此时我已经失败了...

public partial class MainWindow : Window
{
    private Disc disc;

    public MainWindow()
    {
        disc = new Disc();

        DataGridTextColumn radiusColumn = new DataGridTextColumn();
        radiusColumn.Header = "radius";
        radiusColumn.Binding = new Binding("MeasuredRadii") { Source = this.disc };
        MeasureDataGrid.Columns.Add(radiusColumn);
    }
}

When I do this, then I get a blank DataGrid... 当我这样做时,我得到一个空白的DataGrid ...

Specific Questions : 具体问题

  1. How can I bind the disc.MeasuredRadii to the first column of the DataGrid? 如何将disc.MeasuredRadii绑定到DataGrid的第一列?
  2. How can I calculate the other columns with the given Function? 如何使用给定的功能计算其他列?

Important : I don't want to add more Members to the Disc class. 重要提示 :我不想向Disc类添加更多成员。 Because it already holds all the relevant data. 因为它已经保存了所有相关数据。 And I want to do more statics in the table and I don't want to create a field for all these static data, because: What to do if I don't want to show all these statics? 而且我想在表中做更多的静态操作,并且不想为所有这些静态数据创建一个字段,因为:如果我不想显示所有这些静态数据,该怎么办? Then I would have to change the model in cause of a change in the view. 然后,由于视图更改,我将不得不更改模型。 This is against my personal rules... 这违反了我的个人规定...

Edit : I come from Winforms, and I thought I could use something like the CellFormating event to bind my neat little method to the DataGrid. 编辑 :我来自Winforms,我想我可以使用像CellFormating事件之类的东西来将整洁的小方法绑定到DataGrid。 Is there something similar in DataGrids? DataGrids中是否有类似的东西?

First, your binding is not working because you are binding to a collection of doubles, therefore you have no property name. 首先,您的绑定不起作用,因为您要绑定到双打的集合,因此没有属性名称。 Also you have not set the ItemsSource for your DataGrid. 另外,您还没有为DataGrid设置ItemsSource。 This code fixes that issue. 此代码解决了该问题。

        DataGridTextColumn radiusColumn = new DataGridTextColumn();
        radiusColumn.Header = "radius";
        radiusColumn.Binding = new Binding("");
        MeasureDataGrid.Columns.Add(radiusColumn);

        MeasureDataGrid.ItemsSource = disc.MeasuredRadii;

However, I believe want you really should do is to create a collection of a class that holds the properties you want to display. 但是,我相信您真正应该做的是创建一个包含您要显示的属性的类的集合。 That doesn't necessarily mean new members, just properties using your neat functions to calculate the things you need. 这并不一定意味着新成员,而只是使用您的整型函数来计算所需内容的属性。

Here's sort of what I'd suggest (a collection of this class would then replace your MeasuredRadii and MeasurementValues in your Disc class). 这是我的建议(该类的集合将替换Disc类中的MeasuredRadii和MeasurementValues)。 Just a suggestion... 只是一个建议...

public class Measurement
{
    private double measuredRadius = 0.0;
    public double MeasuredRadius { get { return measuredRadius; } set { measuredRadius = value; } }
    public double MeasuredThickness
    {
        get
        {
            var measurementValuesWithCurrentRadius = MeasurementValues.Where(x => x.Position.X == MeasuredRadius);
            var measuredDistances = measurementValuesWithCurrentRadius.Select(x => (DISTANCE_BETWEEN_SENSORS - x.DistanceFromSensor1 - x.DistanceFromSensor2));
            return measuredDistances.Average();
        }
    }

    private ObservableCollection<DistanceMeasurementsResults> measurementValues;
    public ObservableCollection<DistanceMeasurementsResults> MeasurementValues { get { return measurementValues; } }

}

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

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