Question

I would like to bind a DataGrid*Column (in this particular case, a DataGridTextBox) to its data in the code-behind. This is because, depending on a CheckBox's IsClicked property, the Column needs to bind to different collections.

Solutions such as this one all point to the following sort of code:

var binding = new Binding("X");
XColumn.Binding = binding;

Now, I've made use of this sort of code in other parts of my program with success, just not with a DataGrid*Column. With the column, however, this is not working as expected, since in fact all the rows of the column are presenting the X-value of the first element of the collection. This is confirmed when I edit any of the cells and all of them are altered, meaning they are all bound to the same single element of the collection, not to the collection as a whole.

Here is the relevant code:

//This is called whenever the CheckBox EqualToResults is clicked
void ControlBindings()
{
    //only showing for (.IsChecked == true), but the other case is similar
    //and presents the same problems
    if (EqualToResults.IsChecked == true)
    {
        var cable = DataContext as NCable;
        //This is the DataGrid object
        Coordinates.ItemsSource = cable;
        var binding = new Binding("X");
        binding.Source = cable.Points;
        //XColumn is the DataGridTextColumn
        XColumn.Binding = binding;
    }
}

Should it be relevant, here's the relevant code for the NCable class.

public class NCable : DependencyObject, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<NPoint> Points;
    public static DependencyProperty PointsProperty = DependencyProperty.Register("Points", typeof(ICollectionView), typeof(NCable));
    public ICollectionView IPointCollection
    {
        get { return (ICollectionView)GetValue(PointsProperty); }
        set { SetValue(PointsProperty, value); }
    }
    public NCable(string cableName)
    {
        Points = new ObservableCollection<NPoint>();
        for (int i = 0; i < 11; i++)
            Points.Add(new NPoint(1,1));
        IPointCollection = CollectionViewSource.GetDefaultView(Points);
    }
}

EDIT 13/05: I've seen somewhere that one must also set the ItemsSource of the DataGrid in this case, so I've done that as well (edited the original code), but still to no avail. The entire column is still bound to the first element of the collection.

Was it helpful?

Solution

Figured it out. In this case, the DataGrid.ItemsSource must be defined (as per the edit in the oP), but the binding.Source must be left undefined. Therefore, the functional code-behind is

void ControlBindings()
{
    if (EqualToResults.IsChecked == true)
    {
        var cable = DataContext as NCable;
        Coordinates.ItemsSource = cable;
        var binding = new Binding("X");
        //REMOVE binding.Source = cable.Points;
        XColumn.Binding = binding;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top