You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am developing a very simple WPF app using MVVM, Data Annotation, EF Core and Community Toolkit.
Model is `[Table("Companies", Schema = "RHOKSAdmin")]
[Index(nameof(CompanyID), IsUnique = true)]
[Index(nameof(CompanyIdentifier), IsUnique = true)]
public partial class CompanyM : ObservableValidator
{
[Key]
[Column("CompanyID")]
[Comment("Unique Identifier for the Company")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[ObservableProperty]
private int? _companyID;
[Column("Identifier", TypeName = "varchar(20)")]
[Comment("Unique Identifier for the Company")]
[Unicode(false)]
[Display(Name = "Company Abbreviation")]
[Required(ErrorMessage = "Required")]
[StringLength(20, MinimumLength = 2, ErrorMessage = "Min 2, Max 20 characters.")]
[ObservableProperty]
[NotifyDataErrorInfo]
private string? _companyIdentifier;
[Column("Fullname", TypeName = "varchar(100)")]
[Comment("Full Name of the Company")]
[Unicode(false)]
[Display(Name = "Company Name")]
[Required(ErrorMessage = "Required")]
[StringLength(100, MinimumLength = 2, ErrorMessage = "Min 2, Max 100 characters.")]
[ObservableProperty]
[NotifyDataErrorInfo]
private string? _companyFullname;
// Public method to trigger validation
public void ValidateAllPropertiesPublic()
{
ValidateAllProperties();
}`
ViewModel loding from EF is `public async Task LoadDataAsync()
{
try
{
AllCompanies?.Clear();
SelectedCompany = new CompanyM();
SelectedCompany.PropertyChanged += SelectedCompany_PropertyChanged;
LoadingProgressBarVisible = true;
if (AppSettingsM != null)
{
switch (AppSettingsM.GetRequiredSection("ConnectionType").Get<ConnectionType>())
{
case ConnectionType.Home:
AllCompanies = [.. await _rhoksHomeSqlServerDBContext.Companies.ToListAsync()];
break;
case ConnectionType.Test:
AllCompanies = [.. await _rhoksTestSqlServerDBContext.Companies.ToListAsync()];
break;
case ConnectionType.SqlLite:
AllCompanies?.Clear();
break;
case ConnectionType.None:
AllCompanies?.Clear();
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Loading Company Data", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
// Ensure event subscription and validation after setting SelectedCompany
SelectedCompany = AllCompanies?.FirstOrDefault() ?? new CompanyM();
ManualValidation();
LoadingProgressBarVisible = false;
}
}`
Code for SelectedCompany is `private CompanyM? _selectedCompany;
public CompanyM? SelectedCompany
{
get => _selectedCompany;
set
{
if (_selectedCompany != null)
{
_selectedCompany.PropertyChanged -= SelectedCompany_PropertyChanged;
}
private void SelectedCompany_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// Debug statement to check if the event is firing
Console.WriteLine($"PropertyChanged event fired for property: {e.PropertyName}");
CanSaveCompany = SelectedCompany is not null && !SelectedCompany.HasErrors;
SaveVisibility = CanSaveCompany;
}`
My issue is whenever I assign a value to SelectedCompany using the statement SelectedCompany = AllCompanies?.FirstOrDefault() ?? new CompanyM();
I have to run ManualValidation() to get the Validated results via the public void ValidateAllPropertiesPublic() { ValidateAllProperties(); } part of the Model.
SO the validation is not running when doing a SelectedCompany = AllCompanies?.FirstOrDefault() ?? new CompanyM(); and also the `private void SelectedCompany_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// Debug statement to check if the event is firing
Console.WriteLine($"PropertyChanged event fired for property: {e.PropertyName}");
CanSaveCompany = SelectedCompany is not null && !SelectedCompany.HasErrors;
SaveVisibility = CanSaveCompany;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am developing a very simple WPF app using MVVM, Data Annotation, EF Core and Community Toolkit.
Model is `[Table("Companies", Schema = "RHOKSAdmin")]
[Index(nameof(CompanyID), IsUnique = true)]
[Index(nameof(CompanyIdentifier), IsUnique = true)]
public partial class CompanyM : ObservableValidator
{
[Key]
[Column("CompanyID")]
[Comment("Unique Identifier for the Company")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[ObservableProperty]
private int? _companyID;
ViewModel loding from EF is `public async Task LoadDataAsync()
{
try
{
AllCompanies?.Clear();
SelectedCompany = new CompanyM();
SelectedCompany.PropertyChanged += SelectedCompany_PropertyChanged;
}`
Code for SelectedCompany is `private CompanyM? _selectedCompany;
public CompanyM? SelectedCompany
{
get => _selectedCompany;
set
{
if (_selectedCompany != null)
{
_selectedCompany.PropertyChanged -= SelectedCompany_PropertyChanged;
}
}
private void SelectedCompany_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// Debug statement to check if the event is firing
Console.WriteLine($"PropertyChanged event fired for property: {e.PropertyName}");
}`
My issue is whenever I assign a value to SelectedCompany using the statement
SelectedCompany = AllCompanies?.FirstOrDefault() ?? new CompanyM();
I have to run ManualValidation() to get the Validated results via the
public void ValidateAllPropertiesPublic() { ValidateAllProperties(); }
part of the Model.SO the validation is not running when doing a
SelectedCompany = AllCompanies?.FirstOrDefault() ?? new CompanyM();
and also the `private void SelectedCompany_PropertyChanged(object? sender, PropertyChangedEventArgs e){
// Debug statement to check if the event is firing
Console.WriteLine($"PropertyChanged event fired for property: {e.PropertyName}");
}` doesn't run.
is this by design or am I missing something?
Thanks in advance for any information.
Beta Was this translation helpful? Give feedback.
All reactions