I have a ListView which has bound data, and two additional bindings like this:
<ListView x:Name="listViewProducts" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" ItemsSource="{Binding Products}" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding Path=Product, Mode=OneWayToSource}" SelectedIndex="{Binding Path=SelectedProductIndex, Mode=OneWayToSource}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="100" DisplayMemberBinding="{Binding ID}" />
<GridViewColumn Header="Name" Width="170" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Price" Width="100" DisplayMemberBinding="{Binding Price}" />
<GridViewColumn Header="Reseller" Width="Auto" DisplayMemberBinding="{Binding Reseller}" />
</GridView>
</ListView.View>
</ListView>
Like this I have the necessary Product property in the ViewModel. I also have 4 textboxes for example like the following:
< TextBox Grid.Row="0" Grid.Column="1" x:Name="txtName" ...
Text="{Binding Text, Path=Product.Name, Mode=TwoWay}" />
And here is what I’m trying to achieve:
I want to update a listelement only when the user clicks to a button defined like:
<Button Grid.Row="0" Grid.Column="2" x:Name="btnUpdate" ....
Content="Update product"
Command="{Binding Path=Update_Command}"
CommandParameter="{Binding ElementName=listViewProducts, Path=SelectedIndex}" />
And this is what happens: Whenever I select an item in the listView the textboxes gets the right properties. When I change one of the textboxes and chose anotherone, the model gets updated immediately. How is this possible when the selecteditem is bound with “OneWaytoSource” mode?
It seems to me that it happens because you bind SelectedItem to Product property in you ViewModel, after you selected an item in the listView it will pass the reference to Product property through listView.SelectedItem, so you will have the reference to the same object you have in Products collection. After that you are using TwoWay bindings (TextBoxes) and change properties of a Product, which will automatically reflect these changes to the listView through the reference.