我正在尝试动态地更改绑定的延迟:
<TextBox Text="{Binding Text, Delay={Binding BindingDelay}}">
</TextBox>
,但我得到了错误:
a"绑定"不能在类型"绑定"的"延迟"属性上设置。A "绑定"只能设置在一个依赖关系上 依赖项。
有什么方法可以实现这一目标?
使用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
时间相应地变化。
看看这是否解决了您的问题。