2009-08-23 48 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)。

相關問題