E
E
Eugene2017-09-30 20:53:24
WPF
Eugene, 2017-09-30 20:53:24

Why is the data not being updated in the ViewModel?

Good afternoon!
Question: why is there no data change in the ViewModel during actions on the form?
There is a model:

public class Checklist: ViewModel.BaseViewModel<br>
    {<br>
        public string Name { get; set; }<br>
        public bool IsSelected { get; set; }<br>
    }

There is a ViewModel:
public class ColumsViewModel:BaseViewModel
    {
        ObservableCollection<Models.Checklist> listc = new ObservableCollection<Models.Checklist>();
      
        public ObservableCollection<Models.Checklist> Listc
        {
            get { return listc; }
            set { listc = value;
                OnPropertyChanged("IsSelected");
            }
        }
        public ColumsViewModel(ObservableCollection<DataGridColumn> col)
        {
            foreach (DataGridColumn d in col)
            {

                if (d.Header != null)
                {
                    if (d.Visibility == Visibility.Visible)
                    {
                        
                        Listc.Add( new Models.Checklist { IsSelected = true, Name = d.Header.ToString() });
                    }
                    else
                    {
                        Listc.Add(new Models.Checklist { IsSelected = false, Name = d.Header.ToString() });
                    }
                }
            }
        }
    }

The form:
<Window x:Class="WpfApp5.SetColums"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp5"
        mc:Ignorable="d"
        Title="SetColums" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="0.1*"/>
        </Grid.RowDefinitions>
        <ListBox x:Name="ch" ItemsSource="{Binding Listc}" >
            <ListBox.ItemTemplate>
                <HierarchicalDataTemplate>
                    <CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
                </HierarchicalDataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" >
            <Button Content="Применить"  Width="70" Height="20" Margin="0,0,10,0" IsDefault="True" Click="Button_Click"/>
            <Button Content="Отмена"  Width="70" Height="20"  Margin="0,2,0,3"   />
        </StackPanel>
       
    </Grid>
</Window>

There is a ListBox on the form, which is filled with CheckBoxes. Filling is correct.
However, if I remove the selected checkbox, then the update in the ViewModel ( listc ) does not occur.
As I understand listc update is not happening:
In ViewModel on
set { listc = value;
                OnPropertyChanged("IsSelected");
            }

worth a breakpoint.
I start the application in debug mode. And I remove and put ticks in the checkbox. The breakpoint is not hit.
baseviewmodel
public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string prop = "")
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

Please answer in as much detail as possible, because I have been doing wpf and mvvm quite recently.
The general meaning of the task:
I get a collection of datagrid columns.
The user chooses which of the columns he needs.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-10-01
@vakym

I figured it out with the help of the guys from the telegram channel.
In this case, the data update does not occur because the checkbox was not correctly bound.
You must also add ItemsSource="{Binding Listc}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question