error handling in WPF-MVVM / disabled button
I have problem with error handling in view. I use caliburn.micro and MEF.
My VM look like this:
[Export(typeof(IShellViewModel))]
public class ShellViewModel : PropertyChangedBase, IShellViewModel,IDataErrorInfo
{
#region Private members
private User _user;
private Dictionary<string, bool> _validProperties;
private bool _allPropertiesValid;
#endregion
#region Private methods
private void ValidateProperties()
{
if (_validProperties.Values.Any(isValid => !isValid))
{
AllPropertiesValid = false;
return;
}
AllPropertiesValid = true;
}
#endregion
#region Constructor
public ShellViewModel()
{
_user = new User();
_v开发者_开发问答alidProperties = new Dictionary<string, bool> {{"Nick", false}, {"Password", false}};
}
#endregion
#region Properties
public bool AllPropertiesValid
{
get { return _allPropertiesValid; }
set
{
if (_allPropertiesValid != value)
{
_allPropertiesValid = value;
NotifyOfPropertyChange("AllPropertiesValid");
}
}
}
#endregion
#region Implementation of IShellViewModel
public string Nick
{
get { return _user.Nick; }
set
{
_user.Nick = value;
NotifyOfPropertyChange("Nick");
}
}
public string Password
{
get { return _user.Password; }
set
{
_user.Password = value;
NotifyOfPropertyChange("Password");
}
}
public void EmptyLogOn()
{
MessageBox.Show(string.Format("LogOn on server with credential: {0}, {1}", Nick, Password));
}
public void LogOn(string nick, string password)
{
MessageBox.Show(string.Format("LogOn on server with credential: {0}, {1}", nick, password));
}
#endregion
#region Implementation of IDataErrorInfo
public string Error
{
get { return (_user as IDataErrorInfo).Error; }
}
public string this[string propertyName]
{
get
{
string error = (_user as IDataErrorInfo)[propertyName];
_validProperties[propertyName] = String.IsNullOrEmpty(error) ? true : false;
ValidateProperties();
CommandManager.InvalidateRequerySuggested();
return error;
}
}
#endregion
}
If I have som error I set properties AllPropertiesValid on false. I bind this properties on Button properties IsEnabled.
So in View I have this:
<Button IsEnabled="{Binding AllPropertiesValid, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Micro:Message.Attach="EmptyLogOn"
Content="Prihlás ma"
Width="100"
Height="25"
VerticalAlignment="Center"
Grid.Row="2"
Grid.ColumnSpan="2"></Button>
<Label Content="{Binding AllPropertiesValid}" Grid.Row="3"/>
But if properties AllPropertiesValid is false Button is still enabled. I check the value of AllPropertiesValid (I bind this properties on label and label content is false) is false.
What’s wrong ? Thank for advance.
EDIT: In designer is button disabled, but when is loaded window button is enable.
If you are doing MVVM then you should be using ICommand
(or other higher-level variants such as CommandBase
, ...) since you need to do something when button is clicked.
In this case you bind to a command property on the ViewModel, you return false on the CanExecute
in the command and button is disabled. Sometimes you have to call CommandManager.InvalidateRequerySuggested()
.
This does not explain why your code is not working. To be honest, it looks alright to me.
精彩评论