Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does this button click work in WPF MVVM?
    primarykey
    data
    text
    <p>I'm starting to study about WFM MVVM pattern.</p> <p>But I can't understand why this <code>Button</code> click works without binding any event or action.</p> <p><strong>View</strong></p> <pre><code>&lt;Window x:Class="WPF2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:WPF2.ViewModel" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;vm:MainWindowViewModel x:Key="MainViewModel" /&gt; &lt;/Window.Resources&gt; &lt;Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainViewModel}}"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,7,0,0" Name="txtID" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=ListViewProducts,Path=SelectedItem.ProductId}" /&gt; &lt;TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,35,0,0" Name="txtName" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=ListViewProducts,Path=SelectedItem.Name}" /&gt; &lt;TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,61,0,0" Name="txtPrice" VerticalAlignment="Top" Width="178" Text="{Binding ElementName=ListViewProducts,Path=SelectedItem.Price}" /&gt; &lt;Label Content="ID" Grid.Row="1" HorizontalAlignment="Left" Margin="12,12,0,274" Name="label1" /&gt; &lt;Label Content="Price" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,59,0,0" Name="label2" VerticalAlignment="Top" /&gt; &lt;Label Content="Name" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="12,35,0,0" Name="label3" VerticalAlignment="Top" /&gt; &lt;Button Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="141" Height="23" Margin="310,40,0,0" Content="Update" /&gt; &lt;ListView Name="ListViewProducts" Grid.Row="1" Margin="4,109,12,23" ItemsSource="{Binding Path=Products}" &gt; &lt;ListView.View&gt; &lt;GridView x:Name="grdTest"&gt; &lt;GridViewColumn Header="Product ID" DisplayMemberBinding="{Binding ProductId}" Width="100"/&gt; &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="250" /&gt; &lt;GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}" Width="127" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>ViewModel</strong></p> <pre><code>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); } } 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> <p>I found this code from a tutorial i've read. When i click on a row from listview, the textbox values are set. And when I edit those values and click a button, the values on listview are updated.</p> <p>There is no event nor command bound on the button. So how do button click update the values from listview??</p>
    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.
 

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