Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

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

, 14 Jan 2010
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.
Wex.zip
Assemblies
Microsoft.Expression.Interactions.dll
System.ComponentModel.Composition.dll
System.CoreEx.dll
System.Interactive.dll
System.Reactive.dll
System.Windows.Interactivity.dll
WexEventTrigger
ClassDiagram1.cd
Wex.csproj.user
bin
Debug
WexEventTrigger.dll
WexEventTrigger.vshost.exe
WexEventTrigger.vshost.exe.manifest
Composition
Lib
Properties
Settings.settings
Reactive
WexEventTrigger.Demo
bin
Debug
WexEventTrigger.Demo.vshost.exe
WexEventTrigger.Demo.vshost.exe.manifest
Commands
Extensions
Properties
Settings.settings
Views
Demos
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)

About the Author


| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 14 Jan 2010
Article Copyright 2010 by Anoop Madhusudanan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid