3

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!

1
  • 1
    Use “x:DataType”
    – Jason
    Sep 2, 2022 at 1:13

1 Answer 1

2

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

4
  • 1
    Maybe you should add a link to this video. It wil make your answer more helpfull for other users Sep 10, 2022 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, 2022 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, 2022 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
    2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.