AvaloniaUI / avalonia-docs

https://docs.avaloniaui.net/docs/welcome
65 stars 213 forks source link

DataGrid documentation page wrongly states that DataGrid is editable #418

Closed IridiumWasTaken closed 8 months ago

IridiumWasTaken commented 8 months ago

The documentation for DataGrid states the example code given creates an editable DataGrid:

This example shows how the DataGrid can accept changes and update the underlying collection, and use different column types to edit the data:

However, when following the example code given on that page

<DataGrid Margin="20" ItemsSource="{Binding People}"        
          GridLinesVisibility="All"
          BorderThickness="1" BorderBrush="Gray">
  <DataGrid.Columns>
     <DataGridTextColumn Header="First Name"  Binding="{Binding FirstName}"/>
     <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
     <DataGridCheckBoxColumn Header="Fictitious?" Binding="{Binding IsFictitious}" />
  </DataGrid.Columns>
</DataGrid>
using AvaloniaControls.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace AvaloniaControls.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        public ObservableCollection<Person> People { get; }

        public MainWindowViewModel()
        {
            var people = new List<Person> 
            {
                new Person("Neil", "Armstrong", false),
                new Person("Buzz", "Lightyear", true),
                new Person("James", "Kirk", true)
            };
            People = new ObservableCollection<Person>(people);
        }
    }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool IsFictitious { get; set; }

    public Person(string firstName , string lastName, bool isFictitious)
    {
        FirstName = firstName;
        LastName = lastName;
        IsFictitious = isFictitious;
    }
}

The DataGrid is not editable. In this case, the mention that it is editable should be removed.

maxkatz6 commented 8 months ago

DataGrid is editable. Implement INotifyPropertyChanged on your Person model.

maxkatz6 commented 8 months ago

INotifyPropertyChanged might need to be mentioned in the documentation. But control itself is editable.

timunie commented 8 months ago

Maybe you need to set DataGrid IsReadOnly="false"

IridiumWasTaken commented 8 months ago

I believe either mention INotifyPropertyChanged or don't mention that it is editable in the page about the DataGrid. Otherwise it is very confusing for beginners like me.

timunie commented 8 months ago

@IridiumWasTaken that's a general concept of Avalonia for any editable data that you should implement INPC. However it may be worth to mention IsReadOnly property on that page. If you want to file a PR to add it, that's aprreciated.