AvaloniaUI – DataGrid is not shown

Veröffentlicht von

DataGrid with AvaloniaUI does not work.

I had the problem, that the DataGrid component would not show up in my application. I added the DataGrid to a Grid, but nothing was there after running the program. There was also no error during compilation.

How to fix this?

Add package

The first thing is to add the DataGrid component NuGet package to the project. By default, the DataGrid is not available. It is available via a transitive dependency via the “Avalonia.Diagnostics” package. By that, it runs in debug mode, but not in release mode.

Add styles

After that, you need to add the proper styles for the grid to the application. Examples can be found in the documentation.

In my case, I had to add the styles for the “Fluent” theme.

<Application.Styles> 
<FluentTheme Mode="Light"/> 
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/> 
</Application.Styles> 

Column definition

Still nothing would show up. I found out, that I had to define the column definitions. Either by automatically generating the columns or by defining them manually.

AutoGenerateColumns="True"

After that, the DataGrid would finally show up.

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert