Click here to Skip to main content
15,892,161 members
Articles / Desktop Programming / WPF

WPF Extensibility Hacks or WEX - Includes EventTrigger, ReactiveTrigger, InvokeMethodAction, InvokeCommandAction etc.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
14 Jan 2010CPOL4 min read 38K   353   24  
A set of extensibility hacks for WPF. A few interesting triggers and actions, including EventTrigger, ReactiveTrigger, InvokeMethodAction, and InvokeCommandAction. Also allows invoking Triggers and Actions based on Conditions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WexEventTrigger.Lib
{

    /// <summary>
    /// The base class for all Wex framework triggers
    /// </summary>
    public abstract class WexEventTrigger : System.Windows.Interactivity.TriggerBase<DependencyObject>
    {
        protected bool initialized = false;


        #region Properties


        /// <summary>
        /// The data context of the element
        /// </summary>
        public object Model
        {
            get
            {
                if (AssociatedObject is FrameworkElement)
                    return (AssociatedObject as FrameworkElement).DataContext;
                else
                    return null;
            }
        }



        /// <summary>
        /// The type of the source
        /// </summary>
        public ContextTypeEnum SourceType
        {
            get { return (ContextTypeEnum)GetValue(SourceTypeProperty); }
            set { SetValue(SourceTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SourceType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceTypeProperty =
            DependencyProperty.Register("SourceType", typeof(ContextTypeEnum), typeof(EventTrigger), new PropertyMetadata(ContextTypeEnum.Element));




        /// <summary>
        /// Name of the property to listen
        /// </summary>
        public string ElementName
        {
            get { return (string)GetValue(ElementNameProperty); }
            set { SetValue(ElementNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ElementName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ElementNameProperty =
            DependencyProperty.Register("ElementName", typeof(string), typeof(EventTrigger), new PropertyMetadata("This"));

        public string TriggerName
        {
            get { return (string)GetValue(TriggerNameProperty); }
            set { SetValue(TriggerNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TriggerName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TriggerNameProperty =
            DependencyProperty.Register("TriggerName", typeof(string), typeof(EventTrigger), new PropertyMetadata(string.Empty));



        /// <summary>
        /// Actual source of this trigger
        /// </summary>
        protected object Source
        {
            get { return (object)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Source.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(object), typeof(EventTrigger), new PropertyMetadata(null));

        #endregion

        #region IsEnabled


        /// <summary>
        /// Checks whether this item is enabled or not
        /// </summary>
        public bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEnabled.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.Register("IsEnabled", typeof(bool), typeof(EventTrigger), new PropertyMetadata(true));


        #endregion

        #region Methods

        /// <summary>
        /// Hook the loaded event, so that we can initialize once loading is completed
        /// </summary>
        protected override void OnAttached()
        {

            if (!initialized)
            {
                var obj = AssociatedObject as FrameworkElement;
                obj.Loaded += (sender, args) => { InternalInitialize(); };
            }

            base.OnAttached();
        }


        /// <summary>
        /// Initialize the internally
        /// </summary>
        private void InternalInitialize()
        {
            if (AssociatedObject != null && Source == null)
            {
                if (SourceType == ContextTypeEnum.Element)
                {
                    if (ElementName == "This" || string.IsNullOrEmpty(ElementName))
                    {
                        Source = AssociatedObject;
                    }
                    else
                        Source = (this.AssociatedObject as FrameworkElement).FindName(ElementName);
                }
                else if (SourceType == ContextTypeEnum.Model)
                {
                    Source = (AssociatedObject as FrameworkElement).DataContext;
                }
                initialized = true;

                Initialize();

            }

        }

        /// <summary>
        /// Do the trigger initializations here. Should be implemented by all concrete triggers
        /// </summary>
        protected abstract void Initialize();


       
        /// <summary>
        #endregion

    }

    public enum ContextTypeEnum
    {
        Element,
        Model
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions