目前我有一个网格,我正在尝试使用具有验证规则的单元格。为了验证它,我需要行的最小值和最大值。
验证类:
public decimal Max { get; set; }
public decimal Min { get; set; }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var test = i < Min;
var test2 = i > Max;
if (test || test2)
return new ValidationResult(false, String.Format("Fee out of range Min: ${0} Max: ${1}", Min, Max));
else
return new ValidationResult(true, null);
}
用户控制:
<telerik:RadGridView SelectedItem ="{Binding SelectedScript}"
ItemsSource="{Binding ScheduleScripts}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn
DataMemberBinding="{Binding Amount}" Header="Amount"
CellTemplate="{StaticResource AmountDataTemplate}"
CellEditTemplate="{StaticResource AmountDataTemplate}"/>
<telerik:GridViewComboBoxColumn
Header="Fee Type"
Style="{StaticResource FeeTypeScriptStyle}"
CellTemplate="{StaticResource FeeTypeTemplate}"/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
FeeType类:
public class FeeType
{
public decimal Min { get; set; }
public decimal Max { get; set; }
public string Name { get; set; }
}
我在这里尝试了这个解决方案WPF ValidationRule with dependency property,效果很好。但现在我遇到了代理无法通过viewmodel实例化的问题。它基于行选择的ComboBox Value的Min和Max属性。
例如,该组合框样本值低于
Admin Min: $75 Max $500
Late Min: $0 Max $50
由于网格可以拥有几乎所需的行数,因此我无法看到创建代理在我的情况下如何工作。如果我能得到一些指导提示,将不胜感激。
答案 0 :(得分:4)
警告:这不是权威性解决方案,但是向您展示了实现验证逻辑的正确方法,将其完全放在ViewModels上。
为了简洁起见,我将FeeTypes列表创建为FeeType
类的静态属性:
public class FeeType
{
public decimal Min { get; set; }
public decimal Max { get; set; }
public string Name { get; set; }
public static readonly FeeType[] List = new[]
{
new FeeType { Min = 0, Max = 10, Name = "Type1", },
new FeeType { Min = 2, Max = 20, Name = "Type2", },
};
}
这是单个Grid行的ViewModel。我只提供金额和费用属性。
public class RowViewModel : INotifyPropertyChanged, INotifyDataErrorInfo
{
public RowViewModel()
{
_errorFromProperty = new Dictionary<string, string>
{
{ nameof(Fee), null },
{ nameof(Amount), null },
};
PropertyChanged += OnPropertyChanged;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch(e.PropertyName)
{
case nameof(Fee):
OnFeePropertyChanged();
break;
case nameof(Amount):
OnAmountPropertyChanged();
break;
default:
break;
}
}
private void OnFeePropertyChanged()
{
if (Fee == null)
_errorFromProperty[nameof(Fee)] = "You must select a Fee!";
else
_errorFromProperty[nameof(Fee)] = null;
NotifyPropertyChanged(nameof(Amount));
}
private void OnAmountPropertyChanged()
{
if (Fee == null)
return;
if (Amount < Fee.Min || Amount > Fee.Max)
_errorFromProperty[nameof(Amount)] = $"Amount must be between {Fee.Min} and {Fee.Max}!";
else
_errorFromProperty[nameof(Amount)] = null;
}
public decimal Amount
{
get { return _Amount; }
set
{
if (_Amount != value)
{
_Amount = value;
NotifyPropertyChanged();
}
}
}
private decimal _Amount;
public FeeType Fee
{
get { return _Fee; }
set
{
if (_Fee != value)
{
_Fee = value;
NotifyPropertyChanged();
}
}
}
private FeeType _Fee;
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region INotifyDataErrorInfo
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool HasErrors
{
get
{
return _errorFromProperty.Values.Any(x => x != null);
}
}
public IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
return _errorFromProperty.Values;
else if (_errorFromProperty.ContainsKey(propertyName))
{
if (_errorFromProperty[propertyName] == null)
return null;
else
return new[] { _errorFromProperty[propertyName] };
}
else
return null;
}
private Dictionary<string, string> _errorFromProperty;
#endregion
}
现在,我使用原生DataGrid
对其进行了测试,但结果在Telerik中应该是相同的:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Rows}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Amount}"/>
<DataGridComboBoxColumn SelectedItemBinding="{Binding Fee, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{x:Static local:FeeType.List}"
DisplayMemberPath="Name"
Width="200"/>
</DataGrid.Columns>
</DataGrid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Rows = new List<RowViewModel>
{
new RowViewModel(),
new RowViewModel(),
};
DataContext = this;
}
public List<RowViewModel> Rows { get; }
}
如果FeeType
实例可以在运行时修改Min
和Max
,则还需要在该类上实现INotifyPropertyChanged
,并相应地处理值更改。
如果您不熟悉“MVVM”,“ViewModels”,“通知更改”等内容,请查看this article。如果你经常在WPF上工作中大项目,那么学习如何通过MVVM模式解耦View和Logic是值得的。这使您能够以更快,更自动的方式测试逻辑,并保持组织和集中。