Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to set <code>UpdateSourceTrigger</code> to <code>Explicit</code> and on the <code>Click</code> event of the <code>Button</code> you need to update the binding source. </p> <p>The <code>Button</code>s <code>Click</code> event is executed first before <code>Command</code>. Hence the source properties can be updated in the <code>Click</code> event. I have modified your code to demonstrate this. </p> <p>Note: </p> <ol> <li>I have written the view model in code behind for simplicity. </li> <li>I have used the <code>RelayCommand</code> from the <code>MVVMLight</code> library.</li> </ol> <p><strong>1st XAML Change</strong></p> <pre><code> &lt;Button Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="141" Height="23" Margin="310,40,0,0" Content="Update" Click="Button_Click" Command="{Binding UpdateCommand}"/&gt; </code></pre> <p><strong>2nd XAML Change</strong></p> <pre><code> &lt;ListView Name="ListViewProducts" Grid.Row="1" Margin="4,109,12,23" ItemsSource="{Binding Path=Products}" SelectedItem="{Binding SelectedProduct}" &gt; </code></pre> <p><strong>Code behind</strong></p> <pre><code>using GalaSoft.MvvmLight.Command; using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace WPF2 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { txtID.GetBindingExpression(TextBox.TextProperty).UpdateSource(); txtName.GetBindingExpression(TextBox.TextProperty).UpdateSource(); txtPrice.GetBindingExpression(TextBox.TextProperty).UpdateSource(); } } public class MainWindowViewModel : INotifyPropertyChanged { public const string ProductsPropertyName = "Products"; private ObservableCollection&lt;Product&gt; _products; public ObservableCollection&lt;Product&gt; Products { get { return _products; } set { if (_products == value) { return; } _products = value; RaisePropertyChanged(ProductsPropertyName); } } private Product _SelectedProduct; public Product SelectedProduct { get { return _SelectedProduct; } set { _SelectedProduct = value; RaisePropertyChanged("SelectedProduct"); } } private ICommand _UpdateCommand; public ICommand UpdateCommand { get { if (_UpdateCommand == null) { _UpdateCommand = new RelayCommand(() =&gt; { MessageBox.Show( String.Format("From ViewModel:\n\n Updated Product : ID={0}, Name={1}, Price={2}", SelectedProduct.ProductId, SelectedProduct.Name, SelectedProduct.Price)); }); } return _UpdateCommand; } } public MainWindowViewModel() { _products = new ObservableCollection&lt;Product&gt; { new Product {ProductId=1, Name="Pro1", Price=11}, new Product {ProductId=2, Name="Pro2", Price=12}, new Product {ProductId=3, Name="Pro3", Price=13}, new Product {ProductId=4, Name="Pro4", Price=14}, new Product {ProductId=5, Name="Pro5", Price=15} }; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload