question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[WPF] Update button availability

See original GitHub issue

Hi, 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:closed
  • Created 9 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nigel-sampsoncommented, Dec 14, 2014

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) then CanLogin(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.

0reactions
mvermefcommented, Dec 14, 2014

excellent!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found