Answer the question
In order to leave comments, you need to log in
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>
}
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() });
}
}
}
}
}
<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>
set { listc = value;
OnPropertyChanged("IsSelected");
}
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question