Home > OS >  UWP Changing Datagrid and/or DataGridTextColumn Binding Programatically?
UWP Changing Datagrid and/or DataGridTextColumn Binding Programatically?

Time:09-16

I have a datagrid that displays 18 properties of an object that has 149 properties.

6/18 are always displayed for every object.

12/18 change depending on which group of the objects I want to display.

How can I change the binding of the columns I want to change at runtime programmatically.

I've tried

        myBinding.Source = CutterList
        myBinding.Path = New PropertyPath("ToolNumCEBF")
        myBinding.Mode = BindingMode.TwoWay
        myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        BindingOperations.SetBinding(dataGrid1.Columns.Item(1), TextBlock.TextProperty, myBinding)

CutterList is the original ObservableCollection of my datagrid.

myBinding.Path = New PropertyPath("ToolNumCEBF")

I think SHOULD point to a Public Property within my class that is the base of the ObservableCollection, but I'm not sure.

BindingOperations.SetBinding(dataGrid1.Columns.Item(1), TextBlock.TextProperty, myBinding)

As far as I understand is trying to set the binding of my specified column, the text to be displayed, using the other stuff I set with myBinding?

Trying to do this feel like iceskating uphill.

I've also tried just changing my DataGrid's overall itemsource without any luck, so if you have any insights there, I'm all ears.

CodePudding user response:

Figured out a solution. Cast the type of the column from the regular DataGridColumn to DataGridTextColumn. Then it let me set the binding like so:

        Dim tempCol As New DataGridTextColumn
        tempCol = dataGrid1.Columns.Item(1)

        Dim myBinding = New Binding
        myBinding.Source = CutterList
        myBinding.Path = New PropertyPath("ToolNumCEBF")
        myBinding.Mode = BindingMode.TwoWay

        tempCol.Binding = myBinding

This actually doesn't work. It seems to remove the old binding, but something with my Path isn't right. It's not giving an error just producing blank cells.

Ok, actually did figure it out now, a solution, not the reasoning...

For some reason trying to add the .Source and .Mode was unnecessary and causing some kind of issue, when I deleted them, it worked as intended, So i guess as long as your source doesn't change, you can change the binding of a column programmatically like so:

        Dim tempCol As New DataGridTextColumn
        tempCol = dataGrid1.Columns.Item(0)

        Dim myBinding = New Binding
        myBinding.Path = New PropertyPath("ToolNumCEBF")

        tempCol.Binding = myBinding
  • Related