2009-08-23 51 views
2

我想将UpdateSourceTrigger设置为一个控件的事件:当Button.Click被触发时XAML Binding.UpdateSourceTrigger?

<TextBox Text="{Binding Field, UpdateSourceMode=btnOK.Click}"> 
<Button Name="btnOK"> 
    <Button.Triggers> 
     <Trigger> 
      <!-- Update source --> 
     </Trigger> 
    </Button.Triggers> 
</Button> 

我想到了两种方式:

  1. 集UpdateSourceMode或结合一些其他的东西。
  2. 设置一个EventTrigger,按钮点击更新源。

可能的,或者我必须用代码做?

回答

1

您必须使用代码。具体来说:

  1. 设置UpdateSourceTrigger=ExplicitTextBox
  2. 拨打UpdateSource当用户点击Button

但是,您可以将代码放在代码后面或attached behavior中。

+0

肯特,我有办法做Xamly?因为我的表单中有一个巨大的文本框,我希望当用户点击“保存”时它应该更新源文件,我不想获取所有的BE并逐个更新。 – Shimmy 2010-01-20 20:44:41

+0

我想我会使用BindingGroup.UpdateSources()。 – Shimmy 2010-01-20 21:31:19

+0

没有我知道的内置方式。但正如我所说的,你可以编写你自己的附加行为。 – 2010-01-21 07:55:31

1

我知道这已经有一段时间了,但我遇到了同样的问题,并希望分享我的解决方案。希望这会对某人有所帮助。

public class UpdateSourceBehavior : Behavior<System.Windows.Interactivity.TriggerBase> 
{ 
    internal const string TargetElementPropertyLabel = "TargetElement"; 


    static UpdateSourceBehavior() 
    { 
     TargetElementProperty = DependencyProperty.Register 
     (
      TargetElementPropertyLabel, 
      typeof(FrameworkElement), 
      typeof(UpdateSourceBehavior), 
      new PropertyMetadata(null) 
     ); 
    } 


    public static readonly DependencyProperty TargetElementProperty; 


    [Bindable(true)] 
    public FrameworkElement TargetElement 
    { 
     get { return (FrameworkElement)base.GetValue(TargetElementProperty); } 
     set { base.SetValue(TargetElementProperty, value); } 
    } 

    public PropertyPath TargetProperty { get; set; } 


    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     this.InitializeMembers(); 
     base.AssociatedObject.PreviewInvoke += this.AssociatedObject_PreviewInvoke; 
    } 

    protected override void OnDetaching() 
    { 
     base.AssociatedObject.PreviewInvoke -= this.AssociatedObject_PreviewInvoke; 
     base.OnDetaching(); 
    } 


    private void AssociatedObject_PreviewInvoke(object sender, PreviewInvokeEventArgs e) 
    { 
     this.m_bindingExpression.UpdateSource(); 
    } 


    private void InitializeMembers() 
    { 
     if (this.TargetElement != null) 
     { 
      var targetType = this.TargetElement.GetType(); 
      var fieldInfo = targetType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) 
             .FirstOrDefault(fi => fi.Name == this.TargetProperty.Path + "Property"); 

      if (fieldInfo != null) 
       this.m_bindingExpression = this.TargetElement.GetBindingExpression((DependencyProperty)fieldInfo.GetValue(null)); 
      else 
       throw new ArgumentException(string.Format("{0} doesn't contain a DependencyProperty named {1}.", targetType, this.TargetProperty.Path)); 
     } 
     else 
      throw new InvalidOperationException("TargetElement must be assigned to in order to resolve the TargetProperty."); 
    } 


    private BindingExpression m_bindingExpression; 
} 
0

这里是我的解决方案:

XAML:

<StackPanel> 
    <i:Interaction.Triggers> 
    <i:EventTrigger SourceName="submit" EventName="Click"> 
     <behaviours:TextBoxUpdateSourceAction TargetName="searchBox"></behaviours:TextBoxUpdateSourceAction> 
    </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <TextBox x:Name="searchBox"> 
    <TextBox.Text> 
     <Binding Path="SomeProperty" UpdateSourceTrigger="Explicit" NotifyOnValidationError="True"> 
     <Binding.ValidationRules> 
      <DataErrorValidationRule ValidatesOnTargetUpdated="False"/> 
     </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
    </TextBox> 
    <Button x:Name="submit"></Button> 
</StackPanel> 

行为定义(自TargetedTriggerAction继承):

public class TextBoxUpdateSourceAction : TargetedTriggerAction<TextBox> 
{ 
    protected override void Invoke(object parameter) 
    { 
     BindingExpression be = Target.GetBindingExpression(TextBox.TextProperty); 
     be.UpdateSource(); 
    } 
} 

请注意,它TextBoxUpdateSourceAction附加到父很重要容器(示例代码中的StackPanel)。

相关问题