0

id like to bind the width of a datagridcolumn to another datagridcolumn to synchronize them.

I use a MVVM pattern and I implement a observercollection in my viewmodel

 public ObservableCollection<Team_sum> TeamSum
    {
        get
        {
            ObservableCollection<Team_sum> col_teamsum = new ObservableCollection<Team_sum>();
            col_teamsum.Add(new Team_sum());
            return col_teamsum;    

        }
    }

these collection were bind to a datagrid

<DataGrid.Columns>
            <DataGridTextColumn
                Width="{Binding Source={x:Reference Name=Nr},   Path=ActualWidth, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">

            </DataGridTextColumn>

but i got the folowing error Object reference wasnt set to object instance

I get the width value from a second datagrid.

 <DataGridTextColumn
                Header="Nr"
                Binding="{Binding Row_number}"
                IsReadOnly="True"
                x:Name="Nr"/>
  • I find no other solution in this forum – Frederik.Wagener Feb 02 '18 at 10:36
  • You're returning a freshly allocated `ObservableCollection` *each time* the property is accessed. Instead you need to retain a persistent ObservableCollection inside your view model as shown e.g. [here](https://stackoverflow.com/a/26995630). If you don't, external bindings will be lost. I don't know if this is causing your null reference exception however. – dbc Feb 06 '18 at 02:30
  • Same advice was given in comments here: https://stackoverflow.com/questions/48621069/c-sharp-xaml-object-not-set-to-a-object-instance-binding-width-from-a-datagr – dbc Feb 06 '18 at 02:44

0 Answers0