- 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.Demo
- WexEventTrigger.sln
- WexEventTrigger
- bin
- Debug
- ClassDiagram1.cd
- Composition
- Lib
- Properties
- Reactive
- Wex.csproj
- Wex.csproj.user
|
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.
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#