How to get the data of the selected DataGrid row in MVVM? If I understand correctly, the data of the selected DataGrid line falls into the SelectedProduct, and from there it is distributed to the TextBox. Besides all this, I need to get the elements of the selected row ProductName, ProductID, TotalSold (to use them for other purposes and classes). Tried it this way: SelectedProduct.ProductName - does not work.
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } public class ProductModel : INotifyPropertyChanged { private Int32 _ProductID; private String _ProductName; private Int32 _TotalSold; public Int32 TotalSold { get { return _TotalSold; } set { _TotalSold = value; OnPropertyChanged("TotalSold"); } } public String ProductName { get { return _ProductName; } set { _ProductName = value; OnPropertyChanged("ProductName"); } } public Int32 ProductID { get { return _ProductID; } set { _ProductID = value; OnPropertyChanged("ProductID"); } } public ProductModel(Int32 productID, String productName, Int32 totalSold) { this.ProductID = productID; this.ProductName = productName; this.TotalSold = totalSold; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(String propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } public class ProductGridViewModel { private ProductModel _SelectedProduct; private ObservableCollection<ProductModel> _Products; public ObservableCollection<ProductModel> Products { get { return _Products; } set { _Products = value; } } public ProductModel SelectedProduct { get { return _SelectedProduct; } set { _SelectedProduct = value; } } public ProductGridViewModel() { Products = new ObservableCollection<ProductModel>(); GenerateProducts(); } private void GenerateProducts() { for (int x = 0; x < 100; x++) { this.Products.Add(new ProductModel(x, "Product #" + x, x + 50)); } } } <Window x:Class="MVVM.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModel="clr-namespace:MVVM" Title="MainWindow" Height="350" Width="420" Background="Gray"> <Window.Resources> <ViewModel:ProductGridViewModel x:Key="ProductViewModel"/> </Window.Resources> <Grid DataContext="{StaticResource ResourceKey=ProductViewModel}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="300"/> <ColumnDefinition Width="100"/> </Grid.ColumnDefinitions> <DataGrid Width="500" Grid.Column="0" AutoGenerateColumns="False" ItemsSource="{Binding Products}" SelectedItem="{Binding SelectedProduct, Mode=TwoWay}"> <DataGrid.Columns> <DataGridTextColumn IsReadOnly="True" Header="Product ID" Binding="{Binding ProductID, UpdateSourceTrigger=PropertyChanged}" /> <DataGridTextColumn IsReadOnly="True" Header="Product Name" Binding="{Binding ProductName, UpdateSourceTrigger=PropertyChanged}" /> <DataGridTextColumn IsReadOnly="True" Header="Total Sold" Binding="{Binding TotalSold, UpdateSourceTrigger=PropertyChanged}" /> </DataGrid.Columns> </DataGrid> <StackPanel Height="100" Background="Wheat" Margin="10" Orientation="Vertical" Grid.Column="1"> <TextBlock FontWeight="Bold" Width="100" TextWrapping="Wrap">Update your product info!</TextBlock> <TextBox Width="100" Text="{Binding SelectedProduct.ProductName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> <TextBox Width="100" Text="{Binding SelectedProduct.TotalSold, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </StackPanel> </Grid> </Window> 
set { _SelectedProduct = value; }set { _SelectedProduct = value; }- where is the call to thePropertyChangedevent? - BulsonINotifyPropertyChangedimplement - tym32167SelectedProductproperty changes - this seems to be obvious, and then, it does not seem obvious to you that in order "... from there are heard in TextBox ..." these text boxes found that they had new values have appeared and they need to be re-read. - Bulson