1

I have a Telerik RadDatePicker, which I am binding to the SelectedDate property. I want this control to show a validation error when the default date set in the control is removed/deleted. I was able to achieve this, but the problem was that the validation error occurs only when Enter is pressed or when we click outside the control.

Is there a way tell RadDatePicker to update the source without moving the focus? (Tried UpdateSourceTrigger=PropertyChanged, but still it wasnt working)

3 Answers 3

1

You can use a Behavior that catches whatever (keystrokes, value changes, etc.) and force databinding update.

I have written a similar one to use in Silverlight's TextBox, since in Silverlight you can't change the UpdateSourceTrigger like in WPF.

You can use the following code and make the required adjustments:

public class TextBoxUpdateBehavior : Behavior<TextBox>
{
    public TextBoxUpdateBehavior()
    {
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.TextChanged += AssociatedObjectOnTextChanged;
    }

    private void AssociatedObjectOnTextChanged(object sender, TextChangedEventArgs args)
    {
        var bindingExpr = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
        bindingExpr.UpdateSource();
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.TextChanged -= AssociatedObjectOnTextChanged;
    }
}
1

I might be a bit late now, but you can use CurrentDateTimeText property of RadDatePicker

0

To extend zish's answer, I use the RadDatePicker's SelectedDate property bound to my VM's DateTime? SelectedDate property with no validation set in the binding. I use the RadDatePicker's CurrentDateTimeText property bound to my VM's string SelectedDateText property with ValidatesOnErrors=True in the binding. Then, in the VM, I validate that !String.IsNullOrEmpty(SelectedDateText). This gives me the behavior that I want without the need for a behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.