I ve implemented a Reset button on a Detail page in C# MAUI and it works, but I can t figure out why I need to be so explicit in the way I capture the initial data to perform the reset, when requested.
Here s my code:
Simple 3 field data model:
public class MyDataModel
{
[PrimaryKey, AutoIncrement]
public int my_id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
}
Reset button - when clicked, the form refills with the initial data perfectly using this:
private void Reset_Button_Clicked(object sender, EventArgs e)
{
BindingContext = model = InitialData;
}
Detail page constructor - here s where my question is...
private MyDataModel model;
MyDataModel InitialData = new MyDataModel();
public DetailPageMyDataModel MyData)
{
InitializeComponent();
BindingContext = model = MyData;
InitialData = MyData; // Here s what I d like to do, but it doesn t reset the fields
InitialData.first_name = MyData.first_name; // Instead, I have to use these two lines
InitialData.last_name = MyData.last_name; // Why???
}
Why isn t InitialData = MyData
alone setting my InitialData
and giving me the ability to reset my form later?
Unfortunately, it doesn t do anything - the reset doesn t work if I only use that line.
So, instead, I have to use the two lines below to get my reset button to work. (Note: I can remove InitialData = MyData
line as long as I include the other two.
Any help is appreciated!
Thanks