开发者

Update Source Without Losing Focus - WPF Telerik RADDatePicker

开发者 https://www.devze.com 2023-02-13 13:01 出处:网络
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

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 press开发者_运维技巧ed 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)


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;
    }
}


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消