简体   繁体   中英

Binding the Delay property of a Binding

I am trying to dynamically change the delay of a Binding:

<TextBox Text="{Binding Text, Delay={Binding BindingDelay}}">
</TextBox>

But I get the error:

A 'Binding' cannot be set on the 'Delay' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

Is there any way to achieve this?

You cannot change the Binding after it has been used. You can although create a new Binding and apply it.

Now, to apply binding to a non-DP , you can use AttachedProperty , and in its PropertyChangedHandler do whatever you like.

I tested this concept as below and found it working :

// Using a DependencyProperty as the backing store for BoundedDelayProp.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BoundedDelayPropProperty =
        DependencyProperty.RegisterAttached("BoundedDelayProp", typeof(int), typeof(Window5), new PropertyMetadata(0, OnBoundedDelayProp_Changed));

    private static void OnBoundedDelayProp_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBox tb = d as TextBox;
        Binding b = BindingOperations.GetBinding(tb, TextBox.TextProperty);

        BindingOperations.ClearBinding(tb, TextBox.TextProperty);

        Binding newbinding = new Binding();
        newbinding.Path = b.Path;
        newbinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        newbinding.Delay = (int)e.NewValue;

        BindingOperations.SetBinding(tb, TextBox.TextProperty, newbinding);
    }

XAML :

<TextBox local:MyWindow.BoundedDelayProp="{Binding DelayTime}"
         Text="{Binding Time, UpdateSourceTrigger=PropertyChanged, Delay=5000}" />

Delay time changes accordingly.

See if this solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM