How to update DataGrid

Asked by At

The DataContext of the app is bound to my MainViewModel. This ViewModel has a property services and some other properties. When I start a service I want to call OnPropertyChanged("Services") inside my StartService() and RefreshServices()but it is not updating.

My XAML/DataGrid:

        <DataGrid x:Name="dataGrid" Grid.Row="2" Grid.ColumnSpan="8"
                  ItemsSource="{Binding Services}"
                  AutoGenerateColumns="False"
                  IsReadOnly="True">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Display Name"  Binding="{Binding DisplayName, Mode=OneWay}"/>
                <DataGridTextColumn Header="Status" Binding="{Binding Status, Mode=OneWay}" />
                <DataGridTextColumn Header="Machine Name" Binding="{Binding MachineName, Mode=OneWay }" />
                <DataGridTextColumn Header="Can Stop" Binding="{Binding CanStop, Mode=OneWay}" />
            </DataGrid.Columns>
        </DataGrid>

My ViewModel:

namespace ServiceStarterPRAT.ViewModels
{
    public class MainViewModel : ObservableObject
    {
        #region Services Properties
        private ObservableCollection<ServiceController> _services;
        private ObservableCollection<ServiceController> _selectedServices;

        public ObservableCollection<ServiceController> SelectedServices
        {
            get
            {
                return _selectedServices;
            }
            set
            {
                if (_selectedServices == null) return;
                _selectedServices = value;
                OnPropertyChanged("SelectedServices");
            }
        }
        public ObservableCollection<ServiceController> Services
        {
            get
            {
                _services.Clear();
                var curenntServices = Utils.UpdateServices(ComputerNames);
                foreach (var service in curenntServices)
                    if (service.MachineName == SelectedComputer)
                        if (Utils.CustomerNameOf(service) == SelectedCustomer)
                            _services.Add(service);
                return _services;
            }
            set
            {
                //if (_services == null) return;
                _services = value;
                OnPropertyChanged("Services");
                OnPropertyChanged("SelectedServices");
            }
        }

        #endregion

I think I haven't fully understood OnPropertyChanged(). I thought whenever I call something like OnPropertyChanged("Services")anything bound to the property Services will be notified and grab the updated data. However it is not.. what am I doing wrong. Which information do you need besides my XAML?

1 Answers

1
Andy On Best Solutions

One thing you should change is this:

        set
        {
            if (_selectedServices == null) return;
            _selectedServices = value;
            OnPropertyChanged("SelectedServices");
        }

_selectedServices is null initially and it will remain so. Setting it will always fail because it'll always just return.

I suggest you always use curly braces as well for an if, by the way. Always put your statement on the next line.

  if (_selectedServices == null) 
     {
         // this is a terrible idea because it will always be true and your setter stops the property being set
          return;
     }