4

In WPF, I used the normal combination of:

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DataContext="{d:DesignData...}"

To allow a design-time DataContext to be set. This meant that I got reasonable Intellisense in my XAML. I realise the above code doesn't compile but you get the idea.

I've just started using MAUI (without Blazor) and am looking to achieve the same thing - so that I can tell Visual Studio that my ContentView will be bound to a specific object type, ie d:BindingContext="" in a way that will be ignored at runtime? It would make designing ItemTemplates so much easier!

AJ.
  • 1,621
  • 1
  • 10
  • 23

1 Answers1

4

Thanks for Jason's comment. I actually came across the answer to this question in a James Montemango video too.

x:DataType is fantastic - it is stronger than the old system in that it actually provides compile-time protection for the bindings.

Edit: The video in question is: https://youtu.be/3-cT97sBmxM

AJ.
  • 1,621
  • 1
  • 10
  • 23
  • 1
    Maybe you should add a link to this video. It wil make your answer more helpfull for other users – Ivan Kozlov Sep 10 '22 at 15:39
  • Please, add a text sample of final solution, do not let people watch 13 minute video to just find out how to write two lines of code. – bearpro Nov 17 '22 at 20:33
  • Just add x:DataType="someNamespace:SomeViewModel" attribute to the ContentPage and/or DataTemplate elements. You'' probably have to additionally add a clr-namespace attribute to the XAML XML definition for "someNamespace". Also, [assembly: XamlCompilation(XamlCompilationOptions.Compile)] in AssemblyInfo.cs – K0D4 Nov 28 '22 at 19:09
  • For anyone that comes across this in the future I had to do xmlns:viewmodel="clr-namespace:Application.ViewModels" then x:DataType="viewmodel:PageNameViewModel". The exact moment in the video provided is 5:38 if it is still available. – Laim Mar 17 '23 at 18:30