In the question Get data in ViewModel from the delegated object about the following View code with bindings and the MVVM pattern as a whole, I understood the following:
In the display of the data we have, the properties of the instances that are transferred from the VM and the names of the properties of the instances cannot but coincide with the names of the properties of the model. As for the fields for entering new data, then there are already VM properties. After receiving the values of these properties, we use them in the command to add a new instance.
<!-- Отображение имеющихся данных --> <ListView ItemsSource="{Binding Path=staff}"> <ListView.View> <GridView> <!-- ... --> <GridViewColumn DisplayMemberBinding="{Binding PersonStringifiedId, UpdateSourceTrigger=PropertyChanged}"> <GridViewColumn DisplayMemberBinding="{Binding PersonFullName, UpdateSourceTrigger=PropertyChanged}" /> <!-- ... --> <!-- Ввод новых данных --> <TextBox> <TextBox.Text> <Binding Path="personStringifiedId"/> </TextBox.Text> </TextBox> <TextBox> <TextBox.Text> <Binding Path="personFullName"/> </TextBox.Text> </TextBox> <Button Command="{Binding addNewPersonCommand}"/> Unfortunately, the mere understanding of the above was not enough to safely associate a new VM property with the View element. Following the pattern in the answer to the question Adding a checkbox to an automatically generated table, I tried to bind the IsChecked property to the IsSelected attribute of the checkbox. I see no reason to introduce this property into the model, because at the moment it is only in the ViewModel .
<ListView ItemsSource="{Binding Path=Staff}"> <!-- ... --> <GridViewColumn> <GridViewColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" With the following VM code, there is no reaction to changing the checkbox value ( System.Diagnostics.Debug.WriteLine ):
private bool personSelected = false; // по умолчанию public bool IsSelected { get { return personSelected; } set { personSelected = value; System.Diagnostics.Debug.WriteLine(personSelected); OnPropertyChanged("IsSelected"); } } What did I misunderstand in the previous question?
IsSelectedat you where lies?IsChecked="{Binding IsSelected}"just not working? - Andrey NOPUpdateSourceTrigger, for example, here:DisplayMemberBinding="{Binding PersonFullName, UpdateSourceTrigger=PropertyChanged}"- Andrey NOP