R – How to read values from Gridview columns controls in WPF

gridviewtextboxwpf

I have a listview control which has 3 gridview columns. I am filling the list view with a datatable from code behind file. First Gridview column has a label control and the remaining 2 gridview columns has textbox control.

My problem is if the user enters a value in the first Gridview textbox column, the second gridview textbox column should be updated with some value. Is there any way to do that.

And also is there any way to get the value of the label control in the 1st gridview column.

Best Answer

From what I see the WPF GridView is just a view mode (look n feel) for the ListView control.. As with most things in WPF, I'd advise you to do this with the ViewModel pattern.

  • What that means is that you have create a ViewModel object for each row in the Grid. The real model would be your DataTable row. The View model is seeded with data from the real model.. i.e. you create a new ViewModel instance from each row in your DataTable. You then populate the Grid with ViewModel objects
  • The View model exposes properties for each value you need to display in the actual view. So in this case your ViewModel would expose 3 properties (for each column).
  • Wire up your UI Column to bind to the respective properties in the ViewModel.
  • Now your ViewModel.Property1 would contain the logic of updating Property2 with the right computed value on a change. Both of them should trigger off PropertyChanged notifications and the GridView should reflect the updated values in both cells.
Related Topic