У меня проблема с обработкой ошибок. Я использую caliburn.micro и MEF.
Моя ВМ выглядит так:
[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();
_validProperties = 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
}
Если у меня есть ошибка сома, я устанавливаю свойства AllPropertiesValid на false. Я связываю это свойства со свойствами кнопки IsEnabled.
Итак, у меня есть следующее:
<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"/>
Но если для свойства AllPropertiesValid установлено значение false, кнопка по-прежнему включена. Я проверяю значение AllPropertiesValid (я связываю эти свойства с меткой, а содержимое метки ложно).
Что не так? Спасибо за продвижение.
РЕДАКТИРОВАТЬ: В конструкторе кнопка отключена, но при загрузке окна кнопка включена.