Reputation: 385
I have a custom date picker in wpf below is the code of xaml
<local:Customdatepicker x:Uid="dateValue"
x:Name="dateValue"
BorderThickness="0"
Visibility="{Binding ShowDatePicker, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding FieldValueIsEditable, UpdateSourceTrigger=PropertyChanged}" DateCoordinates="{Binding CoOrdinates, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Text="{Binding DateFieldValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, TargetNullValue=''}"
Loaded="dateValue_Loaded"
Style="{StaticResource DatePickerStyle}"
Width="{Binding DatePickerWidth, UpdateSourceTrigger=PropertyChanged}" MouseDoubleClick="dateValue_MouseDoubleClick"
DisplayDate="{Binding DisplayDateFieldValue, Mode=TwoWay}"
LostFocus="dateValue_LostFocus" />
class
public class Customdatepicker : DatePicker {
public Customdatepicker() {
DefaultStyleKey = typeof(DatePicker);
}
public _Coordinates DateCoordinates {
get { return (_Coordinates) GetValue(CoordinatesProperty); }
set { SetValue(CoordinatesProperty, value); }
}
public static readonly DependencyProperty CoordinatesProperty =
DependencyProperty.Register("DateCoordinates", typeof(_Coordinates), typeof(Customdatepicker), null);
}
I want to validate date on Lost focus if I entered wrong date I want to update it but I'm unable to do so. Please let me know how I can do it on LOST FOCUS.
Upvotes: 1
Views: 2254
Reputation: 1649
I think there is some confusion about Binding
and UpdateSourceTrigger
.
First I would bind the SelectedValue
like this instead using x:Name
.
View:
<DatePicker SelectedDate="{Binding Path=SelDate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
ViewModel:
private DateTime _selDate;
public DateTime SelDate {
get { return _selDate; }
set {
if (value.Equals(_selDate)) return;
/*
here you could validate
*/
_selDate = value;
OnPropertyChanged(); //this updates view if changes done programmatically in your ViewModel (that implements INotifyPropertyChanged)
}
}
with this simple Binding
your views DatePicker has the same value as your viewmodels property.
Your validation now could be done in the setter
(see comment) or you use the OnLostFocus
. If you try validation in setter you have to mention that each change calls the setter.
The second possibility is to set the views binding like this:
<DatePicker SelectedDate="{Binding Path=SelDate, UpdateSourceTrigger=LostFocus, Mode=TwoWay}">
With this your setter in viewmodel is called when DatePicker lost its focus. Then you could validate in setter
and it will been performed at LostFocus
.
Hope that helped for understanding.
Edit:
For the current text you could add a several Binding
on Text
:
<DatePicker Text="{Binding DateText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
viewmodel:
...
public string DateText {
get { return _dateText; }
set {
if (value == _dateText) return;
_dateText = value;
OnPropertyChange();
}
}
NOTE: The texts value getting changed automatically on LostFocus an is Empty, if the string is sadfs
.... But with that you can observe the text at the setter. I hope that helps.
Upvotes: 1