Trouble accessing custom field properties in ObservableCollection
Mirai Dinelli
0
Reputation points
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?