【发布时间】:2016-12-20 04:04:55
【问题描述】:
我正在学习 MVVM,到目前为止一切都很好,除非在验证时禁用按钮。我的验证工作完美无缺,所以禁用按钮部分。这是我的代码:
ViewModelBase:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我的验证类:
public class NumberValidation : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (String.IsNullOrEmpty(value.ToString()))
{
return new ValidationResult(false, "No debe estar vacio");
}
double result = 0;
bool canConvert = double.TryParse(value as string, out result);
return new ValidationResult(canConvert, "No es numero valido");
}
}
还有我的 ViewModel:
public class CommercePayment : ViewModelBase
{
private ICommand _SubmitCommand;
private PayCommerce _payCommerce;
public PayCommerce PayCommerces
{
get
{
return _payCommerce;
}
set
{
_payCommerce = value;
NotifyPropertyChanged("PayCommerces");
}
}
public CommercePayment()
{
PayCommerces = new PayCommerce();
}
void PayCommerce_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
NotifyPropertyChanged("PayCommerces");
}
public void Submit()
{
var test = PayCommerces.AccountNumber;
var test2 = PayCommerces.PaymentAmount;
// var test2 = PayCommerces.Commerce.MerchantFullName;
}
#region Helpers
public ICommand SubmitCommand
{
get
{
if (_SubmitCommand == null)
{
_SubmitCommand = new RelayCommand(param => this.Submit(),
null);
}
return _SubmitCommand;
}
}
#endregion
}
}
我的 XML 是这样绑定的:
<Controls:Tile x:Name="tlProcesar" Title=""
TiltFactor="2"
Width="Auto" Height="Auto"
Count="Procesar"
Command="{Binding SubmitCommand}" Margin="185,189,200,-59"
>
</Controls:Tile>
在我的代码隐藏中,我有这个(正在测试和学习):
private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = IsValid(sender as DependencyObject);
}
private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
ViewModel.CommercePayment payments = new ViewModel.CommercePayment();
payments.Submit();
}
private bool IsValid(DependencyObject obj)
{
return !Validation.GetHasError(obj) && LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All(IsValid);
}
如果我将控件绑定更改为:
Command="ApplicationCommands.Save"
然后按钮被禁用,但当然我在 ViewModel 的 Submit() 方法中没有得到任何数据,因为它没有绑定。如果我保持原样,即使验证失败,按钮仍然可以工作。我能做什么?
更新 1:
创造了这个:
public bool IsValid
{
get { return _isValid; }
set
{
_isValid = value;
NotifyPropertyChanged("IsValid");
}
}
现在我想我需要将它与验证联系起来?
【问题讨论】:
-
当使用RelayCommand构造函数的第二个参数时,按钮只启用,如果函数,第二个参数指向返回true。因此,您可以指向 IsValid 函数,而不是使用“null”作为第二个参数。
-
我看不到您的 Command 实现,但应该有一个
CanExecute参数可供您设置,该参数将自动用于确定 Button 是否启用。如果它基于方法而不是像Save_CanExecute方法中的属性,则您可能需要向对象添加 PropertyChange 通知,每当属性更改可能会使对象无效时调用SaveCommand.RaiseCanExecuteChanged()之类的东西。
标签: c# wpf validation mvvm