Reputation: 5322
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?
Upvotes: 1
Views: 708
Reputation: 9827
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.
Upvotes: 3