Dear Community!
I am a bit confused with the DataForm. How can i clear all entries in its textboxes? I have tried creating a new Object for the DataModel behind, leaving the same object and setting all values to null but when i reopen the DataForm all values are set again even though i made sure, that the Values in the Model behind are set to null or Empty string accordingly.
The model:
public class AddUserModel
{
public string Name { get; set; }
public string Email { get; set; }
public string TelephoneNumber { get; set; }
public Role Role { get; set; }
public void Clear()
{
Name = "";
Email = "";
TelephoneNumber = "";
Role = Role.USER;
}
}
The way i use the DataForm:
<syncfusionPopup:SfPopup x:Name="AddUserPopup" MinimumHeightRequest="400" MinimumWidthRequest="600"
IsOpen="{Binding IsAddUserPopupOpen}"
ShowFooter="True"
ShowHeader="True"
ShowCloseButton="True"
HeaderTitle="Add User"
AcceptCommand="{Binding AddUserCommand}"
AcceptButtonText="Submit">
<syncfusionPopup:SfPopup.HeaderTemplate>
<DataTemplate>
<Label VerticalOptions="Center"
HorizontalOptions="Center"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"
Margin="10"
Text="Add vehicle"/>
</DataTemplate>
</syncfusionPopup:SfPopup.HeaderTemplate>
<syncfusionPopup:SfPopup.ContentTemplate>
<DataTemplate>
<ScrollView Margin="10,0">
<dataForm:SfDataForm DataObject="{Binding UserToAdd}"
CommitMode="PropertyChanged"
x:Name="test">
</dataForm:SfDataForm>
</ScrollView>
</DataTemplate>
</syncfusionPopup:SfPopup.ContentTemplate>
</syncfusionPopup:SfPopup>
The Method in the viewModel:
[RelayCommand]
public async Task AddUser()
{
UserRequest request = new UserRequest()
{
name = UserToAdd.Name,
email = UserToAdd.Email,
telephoneNumber = UserToAdd.TelephoneNumber,
//role = (Role)Enum.Parse(typeof(Role), UserToAdd.Role)
role = UserToAdd.Role
};
try
{
await _userState.AddUser(request);
await _userState.GetAllUsers();
}
catch (Exception e)
{
bool result = await _navigationService.DisplayAlert("Error", "Something went wrong!", "Ok", "Close");
if(result)
return;
}
IsAddUserPopupOpen = false;
UserToAdd.Clear();
}
Hi Oliver,
As of now, the MAUI SfDataForm control will automatically update the data object value when changes are made in the editor view. For dynamic model changes, we recommend using the PropertyChanged event for the data object to update the editor. Please refer to the following code snippet for your reference:
|
public MainPage() { InitializeComponent(); (dataForm.DataObject as ContactInfo).PropertyChanged += MainPage_PropertyChanged; } private void MainPage_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (!string.IsNullOrEmpty(e.PropertyName)) { dataForm.UpdateEditor(e.PropertyName); } } |
By subscribing to the PropertyChanged event of your data object (`ContactInfo` in this example), you can update the editor in response to dynamic changes in your model.
We have prepared a simple sample demonstrating this concept. In this sample, the editors are updated with a value on button click, and the PropertyChanged method is called each time the value in the editor is changed to update the underlying data source. Please find the attached sample for your reference.
We hope this helps you. If you have any further concerns or questions, please feel free to let us know.
Regards,
Vidyalakshmi M.
Is there a newer way of clearing the text fields after the contents/model has been consumed?
I tried the sample project, I could never get it to clear more that 2 of the 3 text fields.
Thanks
Hi Oliver,
Thank you for contacting us.
We have reviewed the information you provided and tested the SfDataform control reseting the values of fields. In our testing, the reseting of the field works correctly when an button is clicked. We have prepared a simple sample. Please find the sample attached for your reference.
Example code snippet:
|
public MainPage() { InitializeComponent(); }
private void Button_Clicked(object sender, EventArgs e) { Client.ContactInfo.FirstName = null; Client.ContactInfo.LastName = null; Client.ContactInfo.PhoneNumber = null; } |
Please review this sample and verify whether the issue is reproducible on your end. If not, we kindly request you to either modify our sample to replicate the issue and share the modified sample with us for further analysis or share your sample where the problem occurs. This will enable us to investigate the issue more effectively and provide a suitable solution.
Regards,
Sri Radhesh Nag S