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.
5,181 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,581 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.
11,003 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Mirai Dinelli 0 Reputation points
    2024-07-22T16:08:43.29+00:00

    Finally found a solution:
    when i define the x:DataType="viewmodel:AddOcorrenciaViewModel" in the contentpage all the child elements are set to my viewmodel, the problem is, the properties of my model arent in the viewmodel, they are inside the Model.
    So what i did was:
    I called my models in my content page: xmlns:models="clr-namespace:AppOcorrencias.MVVM.Models"and then, inside the datatemplate i set the datatype for the model:
    <CollectionView ItemsSource="{Binding OcorrenciaCustomFields}" Margin="0,10,0,0">

    <CollectionView.ItemTemplate>

    <DataTemplate x:DataType="models:CustomField">

    <StackLayout Margin="0,5,0,0">

    <Label Text="{Binding Name}" FontSize="12"/>

    <Entry Text="{Binding Value, Mode=TwoWay}"

    IsReadOnly="{Binding Type, Converter={StaticResource FieldTypeToViewConverter}, Mode=OneWay}"/>

    </StackLayout>

    </DataTemplate>

    </CollectionView.ItemTemplate>

    </CollectionView>

    and this solved my problem.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.