Iridium
Iridium

Reputation: 199

Editable DataGrid in Avalonia

I am new to Avalonia and also have never used WPF before, so I also don't know how it works there. I would like to display and edit a DataGrid in Avalonia. Displaying items in the DataGrid works, but it is not editable, meaning that I do not even get the possibility of changing values in the GUI (I cannot, for example, change the state of a checkbox). If I change the DataGrid to an ItemsControl, it becomes editable. What do I need to change to make it editable? This is my code: View.xaml:

<StackPanel Orientation="Horizontal">
    <!-- not working, is not editable
    <DataGrid ItemsSource="{Binding SpectrometerList, Mode=TwoWay}" 
        GridLinesVisibility="All" 
        AutoGenerateColumns="True"
        BorderThickness="1" 
        BorderBrush="Gray"
        IsReadOnly="False">
    </DataGrid>
    -->
        <!-- Is editable -->
    <ItemsControl ItemsSource="{Binding SpectrometerList}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <CheckBox Margin="4"
                          IsChecked="{Binding X}"
                          Content="{Binding SerialNumber}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl> -->
    <StackPanel Orientation="Vertical">
        <Button>1</Button>
        <Button>2</Button>
    </StackPanel>
</StackPanel>

ViewModel.cs:

public ObservableCollection<Spectrometer> SpectrometerList { get; set; }
public SpectrometerViewModel() {
    SpectrometerList = new ObservableCollection<Spectrometer>(Spectrometer.GetSpectrometers());
}

Model.cs:

public class Spectrometer
{
    public byte ID { get; set; }
    public string SerialNumber { get; set; } = string.Empty;
    public byte Reactor { get; set; }
    public bool X { get; set; }

    public static IEnumerable<Spectrometer> GetSpectrometers()
    {
        var spec1 = new Spectrometer { ID = 0, SerialNumber = "Test 1", Reactor = 1, X = true };
        return new[]
        {
            spec1,
            new Spectrometer { ID = 1, SerialNumber = "Test 2", Reactor = 2, X = false},
            new Spectrometer { ID = 2, SerialNumber = "Test 3", Reactor = 3, X = true}
        };
    }
}

Upvotes: 0

Views: 928

Answers (2)

Luk&#225;š Verner
Luk&#225;š Verner

Reputation: 1

I had the same problem with Avalonia version 11.0.6 and it is probably a bug in Avalonia. When I upgraded to newer Avalonia.Controls.DataGrid nuget package then the issue has been solved.

Upvotes: 0

Max Katz
Max Katz

Reputation: 211

Try implementing INotifyPropertyChanged on Spectrometer

Upvotes: 0

Related Questions