【问题标题】:Retrieve BindingGroup in code在代码中检索 BindingGroup
【发布时间】: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


    【解决方案1】:

    BindingGroupMultiBinding 是两个完全不同的东西。如果要显式提交绑定,可以使用:

    BindingOperations.GetMultiBindingExpression(AssociatedObject, TextBox.TextProperty)
        .UpdateSource();
    

    【讨论】:

    • 非常感谢@eli-arbel!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2023-04-05
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多