benruehl / adonis-ui

Lightweight UI toolkit for WPF applications offering classic but enhanced windows visuals
https://benruehl.github.io/adonis-ui/
MIT License
1.71k stars 143 forks source link

Styling DataGrid #29

Closed Bert-Proesmans closed 4 years ago

Bert-Proesmans commented 5 years ago

I have a question about how to style a DataGrid control.

As of today i've been working with AdonisUI for several days. I started out knowing very little about WPF and have had an immense help from the demo project as well as the example projects from Microsoft. Currently i'm working on an application that shows a lot of data which must also be editable (date fields must be set, text fields must be filled in, checkboxes ticked, etc). The obvious choice was to display all information inside a DataGrid control. The demo project shows a GridView control that's automatically styled, but this doesn't happen automatically for the DataGrid control.

I suppose this is for a certain reason, but does that make correctly styling a DataGrid control a hard problem? If possible, i'd like some pointers on how to approach manually styling this control and/or example code (if that's laying around somewhere). When i have this figured out i can contribute it back to the project ofcourse.

Thanks in advance!

EDIT; Almost all search results for (Data)Grid turn up the GridView from ListView. Am I not aware of some general consensus that DataGrid should not be used? I feel like DataGrid is such an easy control for quickly getting started that it's strange i can find little advanced usage about it.

benruehl commented 5 years ago

There is no particular reason that there is no default style for DataGrid included in AdonisUI. Honestly, I kind of forgot that this control even exists and no one asked for it until now.

I don't think that it would be harder to style it than any other control. I would start with an official template from the Microsoft docs that hopefully has everything on board (I also had the opposite case in the past). This shows the different styles that have to be considered and might show important properties that should be taken into account. Then I would replace the style contents step by step with the ones from the AdonisUI's list view style. Most styles can be matched I think, so that ListViewItem becomes DataGridRow etc. But be careful when replacing the styles as one easily removes things like setters, bindings or triggers which then leads to properties having no effect. There might be some things that cannot be matched easily like the style for DataGridCell which does not exist for list view. You might be able to re-use parts of the row style for that one. Put the whole style collection in a ResourceDictionary file and reference it in your App.xaml.

Note that the styles from Microsoft use VisualStateManagers. Those are a convenient way of defining different appearances depending on the visual state, in case you didn't know already. AdonisUI does that using DataTriggers instead, because they support complex conditions which is required for the layering system. So it's fine to just remove all of those.

Hope this helps. Don't hesitate in case you have further questions. You can leave this issue open so that it reminds me to create the style myself someday in case you have no time to contribute it.

PS: I have never heard or read that the DataGrid should be avoided or anything. But I agree that it is less popular than the list view. Maybe it is because it was added later to WPF when everyone had already committed themselves to list view?!

Bert-Proesmans commented 5 years ago

Hi, thanks for the quick reply and the concrete pointers. I'm certainly gonna look into it! Thank you

benruehl commented 4 years ago

closed by #30