0

In my DataGrid I have three columns, number of rows is dynamic. The values for the DataGrid are double arrays. How can I bind each array to his column without creating a new class (I've propertychangedevent on each array)

<DataGrid Name="dataGrid" HorizontalContentAlignment="Center" VerticalContentAlignment="Stretch"
                              AutoGenerateColumns="False" Style="{DynamicResource DataGridStyle1}" 
                              CellEditEnding="dataGrid_Kennlinie_CellEditEnding" BeginningEdit="dataGrid_Kennlinie_BeginningEdit"
                              MaxWidth="500">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="nue" Binding="{Binding nue}" Width="*">
                                <DataGridTextColumn.Foreground>
                                    <SolidColorBrush Color="Black"/>
                                </DataGridTextColumn.Foreground>
                            </DataGridTextColumn>

                            <DataGridTextColumn Header="mue" Binding="{Binding mue}" Width="*"/>
                            <DataGridTextColumn Header="tpc[Nm]" Binding="{Binding MPc}" Width="*" />
....end

nue, mue and MPc are arrays inbetween another class. When I just do

dataGrid.ItemSource = class.nue;

In this class I create the class which includes my needed variables, these are:

private double[] _nue;
    public double[] nue
    {
        get { return _nue; }
        set
        {
            if (_nue == value) return;
            _nue = value;
            OnPropertyChanged("_nue");
        }
    }
    private double[] _mue;
    public double[] mue
    {
        get { return _mue; }
        set
        {
            if (_mue == value) return;
            _mue = value;
            OnPropertyChanged("_mue");
        }
    }
    private double[] _MPc;
    public double[] MPc
    {
        get { return _MPc; }
        set
        {
            if (_MPc == value) return;
            _MPc = value;
            OnPropertyChanged("_MPc");
        }
    }

it sets me the correct number of rows but without values.

Any ideas? Thanks and happy new year

2
  • You should put the code for the class that you use as data source. Commented Dec 31, 2015 at 15:17
  • I don't need the whole class, just three variables. Commented Dec 31, 2015 at 15:25

1 Answer 1

1

In your example you a just binding to one array (nue). Thus the amount of rows is correct, but no data is shown. Because nue.nue, nue.mue etc does not exist. (You should see errors about the missing bindings in the output window.)

The easiest solution would be to restructure your class, and bind an List, Array or ObervableCollection of that class.

If you get the double arrays as input from somewhere: there is no way around to map this data in some other structure, that the DataGrid can handle by default.

// simplified
class Container {
  public double Nue {get; set;}
  public double Mue {get; set;}
  public double MPc {get; set;}
}

ObservableCollection<Container> containers = ...
dataGrid.ItemSource = containers;

// This will now update the grid, just like other operations on the ObservableCollection
containers.Add(new Container {
   Nue = 13.1,
   Mue = 2.23,
   MPc = 0.01
});

The DataGrid xaml can stay as is, the Bindings should work now.

Sign up to request clarification or add additional context in comments.

5 Comments

but then I do not have my propertychanged event on the originals, I just write new variables with the values of my needed ones
Container could implement INotifyPropertyChanged and you can raise OnPropertyChanged on it's properties.
but then there are doubles.. So when I raise an event, it only changes the double value, not the whole array
This is correct. But since it's an ObservableCollection it will updated on Added and Removed values.
Yes binding works, but it is how I said, just "copying" the values I need, but not raising the event to change the arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.