AvaloniaUI / avalonia-docs

https://docs.avaloniaui.net/docs/welcome
60 stars 190 forks source link

Setting the Design-Time DataContext in Code #467

Closed SimonORorke closed 4 months ago

SimonORorke commented 4 months ago

https://github.com/AvaloniaUI/avalonia-docs/issues/466

SimonORorke commented 4 months ago

Needs to be surrounded by an if statement - if (Design.IsDesignMode).

Done.

Consider: "Setting the design-time DataContext in XAML directly is only possible if the ViewModel has a parameterless constructor." It's not strictly correct, but creating a custom MarkupExtension to use a service locator and construct the correct VM is complicated for docs.

Done. To accommodate the point that it's not strictly correct, I replaced the suggested 'directly' with 'by one of the methods shown above'.

Should change the links to be rooted or relative

Done. I went for relative, as I can test that on my computer.

This perhaps should be in how-to-use-design-time-data instead, but up to you / other reviewers.

My rationale for leaving the new documentation on the 'How to Use Live Preview' page:

In relation to this point, I have changed the name of the view model in the example from MyViewModel (following other examples on the page), to MainWindowViewModel. This seems apt, as the technique is most likely to be used with the run-time view model rather than a special design-time view model.

stevemonaco commented 4 months ago

Changes look good. Thanks for the contribution.