How to get value from unknown object where property is known? RRS feed

  • Question

  • Hi. I need to get values from DataGrid without knowing the itemssource BUT I do know DataGridColumns sortmemberPaths.

    So I would like something like: var row1Col1Value = myDataGrid.Items[0].Where(r=>r.property == myDataGridColumn1.SortmemberPath).Select(r=>r.Value).Single();

    For example lets assume that DataGrid.Itemssource = List<Contract>(); AND first column.sortmemberpath = "Customer.Name"

    • Moved by CoolDadTx Monday, August 5, 2013 2:08 PM WPF related
    Monday, August 5, 2013 12:33 PM


All replies

  • There three different DataGrids (one for WinForms, ASP and WPF), but only WPF's DataGrid has a ItemsSource property so I asume you use WPF.

    And in WPF the one rule about accesing the UI from Code is: Don't. You are on the wrong track!
    What you try to do should be done with StyleTriggers, a ItemTempalte or directly on the ItemsSource.

    Trying to access the UI is the single most common mistake for people comming from other UI Technologies and whatever you try to do, there is a 99% chance to do this with code and the actuall collection. Now is the perfect time for you to unlearn this old technique and use the ones WPF is designed for.

    Also, WPF has it's own forum:


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    • Proposed as answer by Pete LakerMVP Monday, August 5, 2013 4:58 PM
    Monday, August 5, 2013 2:06 PM
  • I found a soluton
    • Marked as answer by GaiusBaltar Tuesday, August 6, 2013 5:56 AM
    Tuesday, August 6, 2013 5:56 AM