Hi,
I have a date picker in a datagrid column. I need to do validations on the selected date. Basically I want to make sure the user selects a future date (Today's date or anything above that). If he selects a wrong value he should be shown a message box with some error and also the cell border needs to change to a different color. Can you please help me?
Thanks,
Ranjith
Here is how we can achieve this in XAML:
Define style in the resources:
<Style x:Key="xamDateTimeEditorStyle" TargetType="{x:Type igEditors:XamDateTimeEditor}"> <Setter Property="ValueConstraint"> <Setter.Value> <igEditors:ValueConstraint MinInclusive="{x:Static sys:DateTime.Now}" /> </Setter.Value> </Setter></Style>
And here is how you setup your DataGrid:
<infraDp:XamDataGrid Name="DetailsViewModelXamGrid" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Auto" AutoFit="True" DataSource="{Binding DetailsVieModel}">
I hope this will help, thanks! ~ Shams
Hello,
Thanks a ton!!! It works like a charm.
-Ranjith
Ranjith,
Here is how to create a style and assign it.
Style style = new Style(typeof(XamDateTimeEditor));
ValueConstraint v = new ValueConstraint();
v.MinInclusive = DateTime.Now;
v.ValidateAsType = ValidateAsType.DateTime;
style.Setters.Add(new Setter(XamDateTimeEditor.ValueConstraintProperty,v));
xamDataGrid1.FieldLayouts[0].Fields[0].Settings.EditorStyle = style;
For events, you can try something like this:
void xamDataGrid1_EditModeEnded(object sender, Infragistics.Windows.DataPresenter.Events.EditModeEndedEventArgs e)
{
if (((DateTime)e.Editor.Value).Day < DateTime.Now.Day)
}
Hello Alex,
Can you give me an example code snippet of how to use ValuConstraing property for date validation scenario as mentioned above? I have successfully implemented it for regex and others but not for date. Appreciate you help in advance.
Hello Ranjith,
There are couple of way of achieving this. You can use ValueContraint property of the ValueEditor. You can as well use EditModeEnding/EditModeEnded event to check if the value is valid against your logic.