【问题标题】:Binding the Delay property of a Binding绑定 Binding 的 Delay 属性
【发布时间】:2017-05-03 15:07:07
【问题描述】:

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

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

但我得到了错误:

不能在“绑定”类型的“延迟”属性上设置“绑定”。一种 'Binding' 只能在 DependencyProperty 上设置 依赖对象。

有什么办法可以做到吗?

【问题讨论】:

  • 你确定你知道延迟应该做什么吗?您至少还应该将 UpdateSourceTrigger 设置为 PropertyChanged,否则设置 Delay 毫无意义。除此之外,您必须在延迟更改时建立一个新的绑定。
  • 我删除了与我的问题无关的文本。延迟只是一个属性,我可以绑定到其他属性,所以我想也许也可以绑定到那个属性。

标签: wpf data-binding


【解决方案1】:

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 时间会相应变化。

看看这是否能解决你的问题。

【讨论】:

  • 一个更好的名字是TextBindingDelay。有界是不同的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 2018-07-06
相关资源
最近更新 更多