Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MAUI not finding property in VIew Model List

Tags:

c#

xaml

maui

I have a simple view model where one property contains a model, and another property contains a list of models.

I am able to bind the "Test" model's properties without an issue but I'm not able to get the XAML to recognize that "ListModel" contains a list with its own properties. I have looked at several examples for how to set up the view model and initialize the list correctly before binding it to the view, and while the XAML understands that "ListModel" is a property, I can't get it to recognize that it's a list, and thus it will not compile so that I can at least see if it isn't the intellisense that could be failing for whatever reason.

This is the view model in question with the list named "ListModel"

public class TestViewModel
{        
    public TestModel Test { get; } = new TestModel();
    public List<TestListModel> ListModel { get; set; }


    public TestViewModel()
    {
        Initialize();
    }

    public void Initialize()
    {
        ListModel = new List<TestListModel>();
        ListModel.Add(new TestListModel
        {
            ListProp1 = "First",
            ListProp2 = "Second",
            ListProp3 = "Third"
        });
    }     
}

This is the Model that is being put into a list. It seems like the view isn't seeing these properties.

public class TestListModel
    {        
        public string ListProp1 { get; set; }
        public string ListProp2 { get; set; }
        public string ListProp3 { get; set; }
    }

This is my XAML Currently.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.MainPage"
             xmlns:local="clr-namespace:ViewModels" 
             x:DataType="local:TestViewModel"
    >

    <ScrollView>

        <VerticalStackLayout 
            Spacing="25" 
            Padding="30,0" 
            VerticalOptions="Center">          

            <!--This works-->
            <Entry Text="{Binding Test.Property1}"/>
            <Entry Text="{Binding Test.Property2}"/>
            <Entry Text="{Binding Test.Property3}"/>            
            
            <!--This does not work-->
            <ListView
                ItemsSource="{Binding ListModel}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding ListProp1}"/>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>    
            </ListView>    
        </VerticalStackLayout>    
    </ScrollView>
</ContentPage>
 
like image 566
AngryDev Avatar asked Sep 12 '25 06:09

AngryDev


2 Answers

For anyone stumbling in on this: Jason in the comments has answered the question. The fix was simply to remove x:DataType from the top of the XAML, though I did not remove the "xmlns:local" from it.

What I had was a View Model that had more than just one model in it, which seemed to upset the intellisense when removing x:DataType. Removing it originally prevented the application from compiling because it couldn't find the properties I had in the XAML. Once I cleaned and rebuilt the solution it compiled and worked without a hitch.

like image 132
AngryDev Avatar answered Sep 13 '25 20:09

AngryDev


I was able to fix the error while also keeping the compiled bindings in place by making the ItemSource bind to a List with a private backing field. It seems like the compiler is unable to resolve the list correctly without the private property being there. After i added listModel it compiled. So the issue seems to be that the setter is missing.

    private List<TestListModel> listModel;
    public List<TestListModel> ListModel { get => listModel; set => listModel = value; }
like image 40
Uke Avatar answered Sep 13 '25 21:09

Uke