Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to retrieve a string from a selected cell in a DataGrid. So far I came up with this:

XML
<DataGrid ItemsSource="{Binding Products}" SelectedItem="{Binding SelectedProduct, Mode=OneWayToSource}">
<DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding Path=ProductKey}" ClipBoardContentBinding="{x:Null}" Header="Product Key" IsReadOnly="True"/>
      <DataGridTextColumn Binding="{Binding Path=Price}" ClipBoardContentBinding="{x:Null}" Header="Price" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>


I only need the ProductKey. A fair number of the examples I have found only leads to code behind in the view, I'd like to avoid that. For simplicity, Let's assume my model has 2 properties; "ProductKey"(string) and "Price" (int) Here is my view model:

C#
private List<Product> _products;

public List<Product> Products
{
     get {return _products;}
}

public ProductViewModel()
{
      _products.Add(new Product{ProductKey="101",Price=100});
      _products.Add(new Product{ProductKey="102",Price=25});
      _products.Add(new Product{ProductKey="301",Price=75});
}

private Product _selectedproduct;

public Product SelectedProduct
{
    get {return _selectedproduct;}
    set
       {
          if (_selectedproduct != value)
             _selectedproduct = value;
      }
}


I tried putting a break point in set SelectedProduct, it doesn't seem to launch whenever I click on the ProductKey column of the datagrid.
Posted

CurrentCell property of the DataGrid needs to be bound to the view model instead of SelectedItem. Mode is set to one way so when the selection is changed, the property in the viewmodel is updated.

C#
<datagrid itemssource="{Binding Products}" currentcell="{Binding CurrentCell, Mode=OneWayToSource}">
</datagrid>


C#
private CurrentCellInfo _currentcell;

public CurrentCellInfo CurrentCell
{
     get {return _currentcell;}
     set
       {
           if (value.Column != null && value != _currentcell)
             {
                 _currentcell = value;
                 _selectedproduct = (Product)_currentcell.Item;
                 // optional, in my case, i didn't need to because I am not binding SelectedProduct to the view
                // OnPropertyChanged("SelectedProduct");
             }
       }
}


All that is left is to get the value of whatever properties from _selectedproduct
 
Share this answer
 
Comments
Maciej Los 2-Oct-15 11:11am    
Great, you found a solution!
+5!
Seems you have to set TwoWay mode, but i'm not a specialist... Please, see: How do you handle data grid cell changes with MVVM?[^].

I'd suggest to read this: MVVM and the WPF DataGrid[^]
 
Share this answer
 
Comments
Lyandor 2-Oct-15 4:22am    
The first link demonstrates what happens when a value is updated in a particular cell, in the link's case SomeProperty of CustomObject.

The second link's more suitable to what I want, except that what I am planning to do now is not as complete. However, I noticed there are code behind in the view itself. I am confused as to is it proper to have some code behind in the view or not. Most of the sources I found indicate that there should be little to no code behind.

What I am trying to achieve here is simply trying to get the value of a selected/clicked cell in the DataGrid. What I have found so far involves in either using some mvvm framework like prism or light or modifying the DataGrid (or both in case of the 2nd link)

I'm curious is there no way to get the value of a clicked/selected cell just by passing it on to a viewmodel property?
Maciej Los 2-Oct-15 5:19am    
Well... I think you forgot to add SelectionChange event handler for datagrid.
Please, see:
DataGrid.SelectionChanged Event
WPF 4: DataGrid - cancelling SelectionChange / restoring previously selected row
Lyandor 2-Oct-15 10:58am    
Great read! Thanks a lot! I figured out what was missing. I didn't know the Item in CurrentCellInfo can be "translated". I had suspicion it was there CurrentCellInfo.Item for awhile but couldn't make out what to do with it.

Once again, thanks a lot
Maciej Los 2-Oct-15 11:10am    
You're very welcome. If my answer was helpful, please accept it - formally - to remove from unanswered list. You can accept your solution too.
Lyandor 2-Oct-15 11:11am    
There we go :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900