Is there a way to get XAML binding intellisense in a separate ContentView file used in a ListView?

cmeerencmeeren USMember ✭✭
edited November 7 in Xamarin.Forms

I have a ListView with this item template:

<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <views:ProjectListEntry />
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>

ProjectListEntry is fairly complex and is used in another ListView, so I have it in its own file. I've set it up like this:

<ContentView
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    ...
    BindingContext="{x:Static helpers:DesignTimeData.ProjectListEntryVm}">

As you can see, to get Intellisense, I tried to set BindingContext to a static member on the DesignTimeData class. This works fine for my Pages (where I replace the BindingContext at runtime), but for ContentViews used in ListViews, the ContentView's BindingContext seems to be inherited (from the ViewCell, I guess). This means that the explicit BindingContext on my ContentView will actually override the BindingContext set on the ViewCell by the ListView, and all my list elements will reflect the static design-time data at runtime. But if I remove the BindingContext, I get no intellisense for the members I bind to inside the ContentView file.

Is there a simple way to get Intellisense for bindings in a ContentView like this? (As mentioned, I can't inline the ContentView in the ListView definition, because the ContentView is fairly complex and used in several lists. I also can't use some kind of VM locator, because although I'm using bindings, I'm not using "full" MVVM - I'm using a Redux-like architecture instead. And I guess a VM locator wouldn't work for this case anyway for the same reasons the above doesn't work.)

Best Answer

  • cmeerencmeeren USMember ✭✭
    edited November 23 Accepted Answer

    This solution seems to work fine. In short: Add

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

    and use d:DataContext instead of BindingContext. Going from the example above, it should look like this:

    <ContentView
      xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      ...
      BindingContext="{x:Static helpers:DesignTimeData.ProjectListEntryVm}">
    

    This leads to working binding intellisense from ReSharper and does not cause issues when run.

Answers

  • cmeerencmeeren USMember ✭✭
    edited November 23 Accepted Answer

    This solution seems to work fine. In short: Add

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

    and use d:DataContext instead of BindingContext. Going from the example above, it should look like this:

    <ContentView
      xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      ...
      BindingContext="{x:Static helpers:DesignTimeData.ProjectListEntryVm}">
    

    This leads to working binding intellisense from ReSharper and does not cause issues when run.

Sign In or Register to comment.