我想设置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. 设置 UpdateSourceTrigger=ExplicitTextBox.
  2. 呼叫 UpdateSource 当用户击 Button.

但是,你可以把代码在背后的代码或在一个 附加行为.

其他提示

我知道这已经有一段时间,但我遇到了同样的问题,并想分享我的解决方案。希望这将有助于为人。

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

这里是我的解决办法:

<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到父容器(在这种情况下,例码)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top