سؤال

in my application I have a TextBox binded to a XmlDataProvider using MultiBinding:

<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>

Now, since I want to control the Update source manually, I create a behavior class to "delay" the update:

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;
  }

}

But when the timer raises the AssociatedObject.BindingGroup returns NULL. Could someone tell me where I wrong, or if there is a best way to do this?

Regards,
Daniele.

هل كانت مفيدة؟

المحلول

BindingGroup and MultiBinding are two completely different things. If you want to commit a binding explicitly, you can use:

BindingOperations.GetMultiBindingExpression(AssociatedObject, TextBox.TextProperty)
    .UpdateSource();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top