Trouble accessing custom field properties in ObservableCollection

Mirai Dinelli 0 Reputation points
2024-07-17T18:43:00.8866667+00:00

I created an ObservableCollection in my ViewModel and defined a CustomField class that implements INotifyPropertyChanged. However, when I try to access the properties of the CustomField model in the view, I get an error saying that the members cannot be found. Here is the code I used:

//this is declared in the viewmodel:
public ObservableCollection<CustomField> OcorrenciaCustomFields { get; } = new ObservableCollection<CustomField>();  
//this is the model
public class CustomField : INotifyPropertyChanged
{
    private int id;
    private int fieldId;
    private int ocorrenciaId;
    private string name = string.Empty;
    private string value = string.Empty;
    private string type = string.Empty;
    private List<string> optionsList = new List<string>();

    [AutoIncrement, PrimaryKey]
    public int Id
    {
        get => id;
        set
        {
            id = value;
            OnPropertyChanged();
        }
    }

    public int FieldId
    {
        get => fieldId;
        set
        {
            fieldId = value;
            OnPropertyChanged();
        }
    }

    [Indexed]
    public int OcorrenciaId
    {
        get => ocorrenciaId;
        set
        {
            ocorrenciaId = value;
            OnPropertyChanged();
        }
    }

    public string Name
    {
        get => name;
        set
        {
            name = value;
            OnPropertyChanged();
        }
    }

    public string Value
    {
        get => value;
        set
        {
            value = value;
            OnPropertyChanged();
        }
    }

    public string Type
    {
        get => type;
        set
        {
            type = value;
            OnPropertyChanged();
        }
    }

    public List<string> OptionsList
    {
        get => optionsList;
        set
        {
            optionsList = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Here is the code snippet where I try to access the properties of the CustomField:

<CollectionView ItemsSource="{Binding OcorrenciaCustomFields}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Margin="5">
                <Label Text="{Binding Name}" FontSize="Small"/>
                <ContentView Content="{Binding Type, Converter={StaticResource FieldTypeToViewConverter}}"/>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

I have already done the dependency injection in the code behind and declared the binding context on my view. What more can I do to access the properties of the CustomField model?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,862 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,197 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,615 questions
{count} votes