3

我想将 UpdateSourceTrigger 设置为控件的事件:

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

我想了两个办法:

  1. 在绑定中设置 UpdateSourceMode 或其他一些东西。
  2. 设置一个在按钮单击时更新源的 EventTrigger。

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

4

3 回答 3

1

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

  1. 设置UpdateSourceTrigger=ExplicitTextBox.
  2. UpdateSource当用户点击时调用Button

但是,您可以将代码放在代码后面或附加的行为中。

于 2009-08-23T08:11:07.603 回答
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;
}
于 2011-06-01T08:58:35.487 回答
1

这是我的解决方案:

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)非常重要。

于 2018-02-01T13:39:42.900 回答