[WPF] Update button availability
See original GitHub issueHi, my view-model implements INotifyDataErrorInfo
. My goal is to enable save button only when there are no errors:
public bool CanSave()
{
return HasErrors == false;
}
public async Task Save()
{
//bunch of code...
}
INotifyDataErrorInfo implementation…
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
private void Validate(object value, [CallerMemberName]string propertyName = null)
{
var results = new Collection<ValidationResult>();
var context = new ValidationContext(this) { MemberName = propertyName };
var isValid = Validator.TryValidateProperty(value, context, results);
if (isValid)
{
if (errors.ContainsKey(propertyName))
errors.Remove(propertyName);
RaiseErrorsChanged(propertyName);
}
else
{
var grouping = from result in results
from property in result.MemberNames
group result by property into g
select g;
foreach (var group in grouping)
{
var messages = group.Select(r => r.ErrorMessage).ToList();
if (errors.ContainsKey(group.Key))
errors[group.Key] = messages;
else
errors.Add(group.Key, messages);
RaiseErrorsChanged(group.Key);
}
}
}
public IEnumerable GetErrors(string propertyName)
{
if (!errors.ContainsKey(propertyName))
return null;
return errors[propertyName];
}
public bool HasErrors
{
get { return errors.Any(); }
}
public void RaiseErrorsChanged(string propertyName)
{
if (ErrorsChanged != null)
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
Using Validate
method
private string lastName;
[Required]
[Display(Name = "Sobrenome")]
public string LastName
{
get { return this.lastName; }
set
{
Validate(value);
this.lastName = value;
this.NotifyOfPropertyChange();
}
}
However, the button is always enabled, even if there are errors. I guess the problem is that I need to update button availability whenever an error is raised, but how can I accomplish that?
Issue Analytics
- State:
- Created 9 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Howto Add a Check For Update button to a wpf application
I have a .NET 4.5 WPF app that gets installed using an MSI, generated via a visual studio setup project. Everything works great,...
Read more >updating buttons state after refresh wpf
I have a converter binded with a property "First.JobLight", this property gets updated by my background thread and tells the button in witch ......
Read more >trying to update button text once every second : r/PowerShell
Need WPF help --- trying to update button text once every second. Hey guys, I'm testing out a simple wpf window which has...
Read more >Custom Control in wpf GridControl doesn't show update ...
I tried two attempts to cause the update and cancel buttons to appear when I change values in the custom control. But unfortunately,...
Read more >The UpdateSourceTrigger property
With the UpdateSourceTrigger property, you control when the source of a binding is updated. This article describes this concept thoroughly, with plenty of ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Pretty much everything @mvermef said is correct here.
There’s a little guidance I use when deciding if the Guard should be a method or a property. If your actual method is taking parameters such as
Login(string username, string password)
thenCanLogin(string username, string password)
makes sense.However if the
Can*
method depends on any other property than the parameters then it should be a property where you can trigger property changed notifications when that property changes.excellent!