Hi Xamarin !
I have a very difficult problem.
I have a ListView with grouping and lot of DataTemplate. I need to add item in this list when user execute an action.
The row I need to add have a label and a Switch, but when I add this item, the row appear with the Switch but the label appear empty.
I work on this since 3 hours and I didn't find any solution (so bad).
This is the source code (I've simplify at maximum) :
Model and grouping :
`
public enum ModelType {
BASE, CHECKBOX, TEXT
}
public class NewHorseEntryModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; // I use Fody here, but it isn't important
public ModelType Type { get; set; }
public string Field { get; set; }
public string Value { get; set; }
public bool Checked { get; set; }
public static NewHorseEntryModel NewBase(string id, string field, string value, string image=null) {
return new NewHorseEntryModel() { FieldID = id, Field = field, Value = value, ImageSource=image, Type = ModelType.BASE };
}
public static NewHorseEntryModel NewText(string id, string field, string value, string image = null)
{
return new NewHorseEntryModel() { FieldID = id, Field = field, Value = value, ImageSource = image, Type = ModelType.TEXT };
}
public static NewHorseEntryModel NewCheckbox(string id, string field, bool isChecked)
{
return new NewHorseEntryModel() { FieldID = id, Field = field, Checked = isChecked, Type = ModelType.CHECKBOX };
}
}
public class EntryGroup : ObservableCollection<NewHorseEntryModel>
{
public string Title { get; set; }
public EntryGroup(string title)
{
Title = title;
}
}
`
The code of the page who show the listView :
`
public partial class NewHorsePage : ContentPage
{
public ObservableCollection AllItems { private set; get; }
public NewHorsePage(Cheval cheval=null)
{
InitializeComponent();
listView.HasUnevenRows = true;
listView.ItemSelected += ItemSelected;
AllItems = new ObservableCollection<EntryGroup> {
new EntryGroup ("CARACTERISTIQUES") {
NewHorseEntryModel.NewBase("race", "RACE", "Auxois"),
NewHorseEntryModel.NewBase("robe", "ROBE", "Alezan"),
NewHorseEntryModel.NewBase("sexe", "SEXE", "Male")
},
new EntryGroup ("GROUPE") {
NewHorseEntryModel.NewBase("groupe")
},
};
// the variable groupGroup represent the EntryGroup who are named "GROUPE"
groupGroup.Insert(0, NewHorseEntryModel.NewCheckbox("groupe1","GROUPE1", false)); // It's work
listView.BindingContext = this;
listView.SetBinding(ListView.ItemsSourceProperty, "AllItems");
// In the constructor if I call this, since I have already bind ItemsSource, it's work :
groupGroup.Insert(0, NewHorseEntryModel.NewCheckbox("groupe1","GROUPE1", false)); // It's work
}
void ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SEE HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// The real problem is here : the row appear but the label is shown empty (the Switch appear correctly)
// If I check the Field property (who are binded to the label in XAML) I got the good value ("GROUPE1")
groupGroup.Insert(0, NewHorseEntryModel.NewCheckbox("groupe1","GROUPE1", false)); // It's work
// NOTE : If I set the Text of the label at "Test" for example (so without binding), the result is the same : the text doesn't appear
}
}
`
So if you have some idea to help me ...
Thanks, and sorry if you didn't understand me very well, english is a lot difficult for me.
Note : I've also try to do :
listView.ItemsSource = null; listView.ItemsSource = AllItems;
But without success.