1
votes

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?

1
Are you sure you know what Delay is supposed to do? You should at least also set UpdateSourceTrigger to PropertyChanged, otherwise setting Delay is pointless. Besides that, you would have to establish a new Binding when the Delay changes.Clemens
I removed the text that is not relevant to my question. The delay is just a property, I can bind to other properties, so I thought maybe it is possible to bind to that property, too.flayn

1 Answers

3
votes

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.