繁体   English   中英

绑定绑定的Delay属性

[英]Binding the Delay property of a Binding

我正在尝试动态更改绑定的延迟:

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

但是我得到了错误:

不能在“绑定”类型的“延迟”属性上设置“绑定”。 只能在DependencyObject的DependencyProperty上设置“绑定”。

有什么办法可以做到这一点?

使用Binding后,您将无法更改它。 尽管可以创建一个新的Binding并应用它。

现在,要将binding应用于non-DP ,可以使用AttachedProperty ,并在其PropertyChangedHandler执行所需的操作。

我如下测试了这个概念,发现它可以正常工作:

// 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时间会相应更改。

看看这是否可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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