【发布时间】:2014-02-19 09:05:21
【问题描述】:
在我的应用程序中,我有一个使用 MultiBinding 绑定到 XmlDataProvider 的 TextBox:
<TextBox Name="TextBox_BaseId"
Grid.Row="0"
Grid.Column="1"
MaxLength="8"
Style="{StaticResource textBoxInError}">
<i:Interaction.Behaviors>
<behaviors:DelayedUpdate />
</i:Interaction.Behaviors>
<TextBox.Text>
<MultiBinding Converter="{StaticResource BaseIDConverter}"
UpdateSourceTrigger="Explicit">
<Binding Source="{StaticResource dataProvider}" XPath="BLOCK[@id=1]/ITEMS/ITEM[(@id=12) and (@index=0)]/@value"/>
<Binding Source="{StaticResource dataProvider}" XPath="BLOCK[@id=1]/ITEMS/ITEM[(@id=12) and (@index=1)]/@value"/>
<Binding Source="{StaticResource dataProvider}" XPath="BLOCK[@id=1]/ITEMS/ITEM[(@id=12) and (@index=2)]/@value"/>
<Binding Source="{StaticResource dataProvider}" XPath="BLOCK[@id=1]/ITEMS/ITEM[(@id=12) and (@index=3)]/@value"/>
<MultiBinding.ValidationRules>
<local:RangeValidator Min="1" Max="16215777" />
</MultiBinding.ValidationRules>
</MultiBinding>
</TextBox.Text>
</TextBox>
现在,由于我想手动控制更新源,我创建了一个行为类来“延迟”更新:
public class DelayedUpdate : Behavior<TextBox>
{
Timer _timer = new Timer(1000);
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.KeyUp += AssociatedObject_KeyUp;
_timer.Elapsed += _timer_Elapsed;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
_timer.Elapsed -= _timer_Elapsed;
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
_timer.Enabled = false;
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
AssociatedObject.BindingGroup.CommitEdit();
}),
System.Windows.Threading.DispatcherPriority.Normal);
}
void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
{
_timer.Enabled = true;
}
}
但是当定时器启动时,AssociatedObject.BindingGroup 返回 NULL。 有人能告诉我我哪里错了吗,或者是否有最好的方法来做到这一点?
问候,
丹尼尔。
【问题讨论】:
标签: c# wpf multibinding