user2412672
user2412672

Reputation: 1479

How to save the item from the collection when property changes?

I have a DataGrid that displays list of sports and I want to save sport when IsActive property changes. How could I do it in MVVM style? I'm using MVVM Light framework.

This is my DataGrid

         <DataGrid ItemsSource="{Binding SportsList}" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Foreground="Black" Width="200" Header="Name" Binding="{Binding Name, Mode=TwoWay}" IsReadOnly="True" />
                <DataGridCheckBoxColumn Width="60" Header="Is Active" Binding="{Binding IsActive, Mode=TwoWay}" IsReadOnly="False" />
            </DataGrid.Columns>
        </DataGrid>

This how view model looks:

public class MainViewModel : ViewModelBase
{
    private readonly ISportService _sportService;

    private ObservableCollection<SportModel> sports;

    public MainViewModel(ISportService sportService)
    {
        _sportService = sportService;
        sports = new ObservableCollection<SportModel>();
        LoadSportsCommand = new RelayCommand(LoadSports);
        SaveSportCommand = new RelayCommand(SaveSport);
    }

    public ICommand LoadSportsCommand { get; private set; }
    public ICommand SaveSportCommand { get; private set; }

    public ObservableCollection<SportModel> SportsList
    {
        get
        {
            return sports;
        }
    }

    private void SaveSport()
    {
            // Todo
    }

    private void LoadSports()
    {
        sports.Clear();
        foreach (var sport in _sportService.GetAllSports())
        {
            sports.Add(new SportModel()
            {
                Name = sport.Name,
                IsActive = sport.IsActive
            });
        }
    }
}

This is SportModel:

public class SportModel : ObservableObject
{    
    public string Name { get; set; }

    public bool IsActive { get; set; }
}

Upvotes: 0

Views: 53

Answers (1)

rmojab63
rmojab63

Reputation: 3629

Some modifications can solve the issue:

private void LoadSports()
{
    // ... your code
    foreach (var r in SportsList)
        r.PropertyChanged += A_PropertyChanged;
}

private void A_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
     SaveSport((SportModel)sender);
}

private void SaveSport(SportModel sport)
{
     // Todo
}

public class SportModel : ObservableObject
{
    public string Name { get; set; }

    bool _isactive;
    public bool IsActive
    {
        get { return _isactive; }
        set
        {
            if (_isactive != value)
            {
                _isactive = value;
                RaisePropertyChanged("IsActive");
            }
    }
}
protected override void RaisePropertyChanged([CallerMemberName] string propertyName = null)
 {
     base.RaisePropertyChanged(propertyName);
 }
}

And its better to use UpdateSourceTrigger=PropertyChanged in your Xaml.

 <DataGridCheckBoxColumn Width="60" Header="Is Active" Binding="{Binding IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" />

Upvotes: 1

Related Questions