AvaloniaUI – DataGrid is not displayed

DataGrid with AvaloniaUI does not work.

I had the problem that the DataGrid component did not show up in my application. I added the DataGrid to a grid, but nothing showed up after running the program. There was also no error when compiling.

How to fix this?

Add package

The first thing to do is to add the DataGrid component’s NuGet package to the project. By default, the DataGrid is not available. It is available via a transitive dependency on the „Avalonia.Diagnostics“ package. This makes it run in debug mode, but not in release mode.

.

Add styles

After that, you need to add the right 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 theme „Fluent“.

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

Column definition

Still nothing was displayed. I found that I needed to define the column definitions. Either by generating the columns automatically or by defining them manually.

AutoGenerateColumns="True"

After that, the DataGrid was finally displayed.

Leave a Reply

Your email address will not be published. Required fields are marked *