Home > other >  Adding x:DataType to page xaml leads to errors in ListView DataTemplate properties
Adding x:DataType to page xaml leads to errors in ListView DataTemplate properties

Time:01-16

I have set ViewModel from code behind for Dependency Injection. I would like to leave possibility for IntelliSense suggestions in xaml. Everything seems to work, but once I am adding x:DataType="viewModels:HomeViewModel", I am getting an error for Number property not found and not able to run my solution. Why so and how to fix this?

If I delete x:DataType="viewModels:HomeViewModel", everything works fine.

HomePage.xaml:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Pages.HomePage"
             xmlns:viewModels="clr-namespace:MyApp.ViewModels"
             x:DataType="viewModels:HomeViewModel"
             Title=""
             NavigationPage.HasNavigationBar="False">

      <Frame>
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Label Grid.Row="0"></Label>
          <ListView ItemsSource="{Binding TotalData}" Grid.Row="1">
            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                  <Grid Padding="5">
                    <Label Text="{Binding Number}" Margin="0,0,10,0"/>
                  </Grid>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>
        </Grid>
      </Frame>
</ContentPage>

EDIT:

Based on Jason answer my HomePage.xaml should have been:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Pages.HomePage"
             xmlns:viewModels="clr-namespace:MyApp.ViewModels"
             xmlns:models="clr-namespace:MyApp.Models"
             x:DataType="viewModels:HomeViewModel"
             Title=""
             NavigationPage.HasNavigationBar="False">

      <Frame>
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Label Grid.Row="0"></Label>
          <ListView ItemsSource="{Binding TotalData}" Grid.Row="1">
            <ListView.ItemTemplate>
              <DataTemplate x:DataType="models:DataModel">
                <ViewCell>
                  <Grid Padding="5">
                    <Label Text="{Binding Number}" Margin="0,0,10,0"/>
                  </Grid>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>
        </Grid>
      </Frame>
</ContentPage>

CodePudding user response:

when you add

x:DataType="viewModels:HomeViewModel"

to the page, VS assumes that this applies to the entire page. It is currently not "smart" enough to recognize that there is a templated control in the page that will have a different DataType

There are two ways to address this. One, you can just remove DataType entirely. It is not required, and is just a helper that VS uses to provide Intellisense to the XAML bindings

Alternatively, you can add another DataType to your templated controls that tell VS what types are being used for those controls.

<DataTemplate x:DataType="viewModels:MyModelClass">

you will need to use the correct xmlns and class name for your code

  • Related