1
我使用MultiBinding綁定到一個XmlDataProvider一個TextBox:在我的應用檢索BindingGroup代碼
<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。 有人能告訴我我錯在哪裏,或者如果有最好的方法來做到這一點?
Regards,
Daniele。
非常感謝@ eli-arbel! – Barzo