交互触发条件和其他适配器

时间:2012-12-12 18:15:57

标签: wpf mvvm

我需要使用一些带有交互触发器的附加条件,例如:

对事件做出反应我想执行命令,但仅在条件匹配时才执行。此外,我希望命令将执行一些延迟。我可以用伪xaml来表达这个。

<ci:RoutedEventTrigger Event="{x:Static Selector.SelectionChangedEvent}">
  <li:TriggerCondition ActualValue="{Binding Value}" ExpectedValue="{StaticResource foo}">
    <li:DelayAction DelayTime="{StaticResource delayTime}">
       <InvokeCommandAction CommandName="{StaticResourc commandName}"/>
    </li:DelayAction>
  </li:TriggerCondition>
</ci:RoutedEventTrigger>

我尝试编写TriggerCondition和DelayAction类,但几乎所有的Interactivity代码都是内部的。

所以问题是如何达到预期的行为? 编写具有条件和延迟的RoutedEventTrigger看起来不那么通用,而且我有InputBindingTrigger等等。

提前致谢

更新 我用反射做了我想要的东西,但我仍在寻找你的建议如何以正确的方式做到这一点:)

  [ContentProperty("Actions")]
  public abstract class TriggerDecorator : TriggerAction<DependencyObject>
  {
    private static readonly DependencyPropertyKey ActionsPropertyKey = 
      DependencyProperty.RegisterReadOnly("Actions", typeof (TriggerActionCollection),typeof (TriggerDecorator), new FrameworkPropertyMetadata());
    public static readonly DependencyProperty ActionsProperty = ActionsPropertyKey.DependencyProperty;
    public TriggerActionCollection Actions
    {
      get { return (TriggerActionCollection) GetValue(ActionsProperty); }
    }

    private readonly MethodInfo myTriggerAction_CallInvokeMethod;

    public TriggerDecorator()
    {
      var triggerActionType = typeof (TriggerAction);
      myTriggerAction_CallInvokeMethod = triggerActionType.GetMethod("CallInvoke", BindingFlags.Instance | BindingFlags.NonPublic);
      if(myTriggerAction_CallInvokeMethod == null)
        throw new InvalidOperationException();

      var actionCollection = (TriggerActionCollection)Activator.CreateInstance(typeof(TriggerActionCollection), true);
      SetValue(ActionsPropertyKey, actionCollection);
    }

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

      if(AssociatedObject == null)
        return;
      Actions.Attach(AssociatedObject);
    }

    protected override void OnDetaching()
    {
      base.OnDetaching();
      Actions.Detach();
    }

    protected void ExecuteActions(object parameter)
    {
      var param = new[] {parameter};
      foreach (var triggerAction in Actions)
        myTriggerAction_CallInvokeMethod.Invoke(triggerAction, param);
    }
  }

1 个答案:

答案 0 :(得分:0)

您可以制作自定义EventTrigger

这样的事情应该有效:

  public sealed class DelayAction : System.Windows.Interactivity.EventTrigger
    {
        private EventArgs _eventArgs;
        private DispatcherTimer _delayTimer;

        public static readonly DependencyProperty DelayProperty =
            DependencyProperty.Register("Delay", typeof(double)
            , typeof(DelayAction), new PropertyMetadata(0.0));

        public double Delay
        {
            get { return (double)base.GetValue(DelayProperty); }
            set { base.SetValue(DelayProperty, value); }
        }

        protected override void OnDetaching()
        {
            if (_delayTimer != null)
            {
                _delayTimer.Stop();
                _delayTimer = null;
            }
            base.OnDetaching();
        }

        protected override void OnEvent(EventArgs eventArgs)
        {
            if (_delayTimer != null)
            {
                _delayTimer.Stop();
            }
            _eventArgs = eventArgs;
            _delayTimer = new DispatcherTimer();
            _delayTimer.Interval = TimeSpan.FromMilliseconds(this.Delay);
            _delayTimer.Tick += new EventHandler(TimerTick);
            _delayTimer.Start();
        }

        private void TimerTick(object sender, EventArgs e)
        {
            this._delayTimer.Stop();
            base.InvokeActions(this._eventArgs);
        }
    }

用法:

<Button Click="Button_Click">
    <i:Interaction.Triggers>
        <myTriggers:DelayAction Delay="1000" EventName="Click">
         // do stuff
        </myTriggers:DelayAction>
    </i:Interaction.Triggers>
</Button>