[英]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.