Skip to content

Commit

Permalink
Merge pull request #99 from nigel-sampson/feature/triggers
Browse files Browse the repository at this point in the history
Adds an interface for triggers. Resolves #20
  • Loading branch information
nigel-sampson authored Jun 14, 2016
2 parents 56b1d7a + d03363c commit 971031a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,8 @@ namespace Microsoft.Xaml.Interactions.Core
/// A behavior that performs actions when the bound data meets a specified condition.
/// </summary>
[ContentPropertyAttribute(Name = "Actions")]
public sealed class DataTriggerBehavior : Behavior
public sealed class DataTriggerBehavior : Trigger
{
/// <summary> q
/// Identifies the <seealso cref="Actions"/> dependency property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly DependencyProperty ActionsProperty = DependencyProperty.Register(
"Actions",
typeof(ActionCollection),
typeof(DataTriggerBehavior),
new PropertyMetadata(null));

/// <summary>
/// Identifies the <seealso cref="Binding"/> dependency property.
/// </summary>
Expand Down Expand Up @@ -55,24 +45,6 @@ public sealed class DataTriggerBehavior : Behavior
typeof(DataTriggerBehavior),
new PropertyMetadata(null, new PropertyChangedCallback(DataTriggerBehavior.OnValueChanged)));

/// <summary>
/// Gets the collection of actions associated with the behavior. This is a dependency property.
/// </summary>
public ActionCollection Actions
{
get
{
ActionCollection actionCollection = (ActionCollection)this.GetValue(DataTriggerBehavior.ActionsProperty);
if (actionCollection == null)
{
actionCollection = new ActionCollection();
this.SetValue(DataTriggerBehavior.ActionsProperty, actionCollection);
}

return actionCollection;
}
}

/// <summary>
/// Gets or sets the bound object that the <see cref="DataTriggerBehavior"/> will listen to. This is a dependency property.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@ namespace Microsoft.Xaml.Interactions.Core
/// A behavior that listens for a specified event on its source and executes its actions when that event is fired.
/// </summary>
[ContentPropertyAttribute(Name = "Actions")]
public sealed class EventTriggerBehavior : Behavior
public sealed class EventTriggerBehavior : Trigger
{
/// <summary>
/// Identifies the <seealso cref="Actions"/> dependency property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly DependencyProperty ActionsProperty = DependencyProperty.Register(
"Actions",
typeof(ActionCollection),
typeof(EventTriggerBehavior),
new PropertyMetadata(null));

/// <summary>
/// Identifies the <seealso cref="EventName"/> dependency property.
/// </summary>
Expand Down Expand Up @@ -61,24 +51,6 @@ public EventTriggerBehavior()
{
}

/// <summary>
/// Gets the collection of actions associated with the behavior. This is a dependency property.
/// </summary>
public ActionCollection Actions
{
get
{
ActionCollection actionCollection = (ActionCollection)this.GetValue(EventTriggerBehavior.ActionsProperty);
if (actionCollection == null)
{
actionCollection = new ActionCollection();
this.SetValue(EventTriggerBehavior.ActionsProperty, actionCollection);
}

return actionCollection;
}
}

/// <summary>
/// Gets or sets the name of the event to listen for. This is a dependency property.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/BehaviorsSDKManaged/Microsoft.Xaml.Interactivity/ITrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.Xaml.Interactivity
{
/// <summary>
/// Interface implemented by all custom triggers.
/// </summary>
public interface ITrigger : IBehavior
{
/// <summary>
/// Gets the collection of actions associated with the behavior.
/// </summary>
ActionCollection Actions { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@
<Compile Include="IAction.cs" />
<Compile Include="IBehavior.cs" />
<Compile Include="Interaction.cs" />
<Compile Include="ITrigger.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\Version\Version.cs" />
<Compile Include="ResourceHelper.cs" />
<Compile Include="Trigger.cs" />
<Compile Include="TriggerOfT.cs" />
<Compile Include="TypeConstraintAttribute.cs" />
<Compile Include="VisualStateUtilities.cs" />
<EmbeddedResource Include="Properties\Microsoft.Xaml.Interactivity.rd.xml" />
Expand Down
43 changes: 43 additions & 0 deletions src/BehaviorsSDKManaged/Microsoft.Xaml.Interactivity/Trigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Windows.UI.Xaml;

namespace Microsoft.Xaml.Interactivity
{
/// <summary>
/// A base class for behaviors, implementing the basic plumbing of ITrigger
/// </summary>
/// <typeparam name="T">The object type to attach to</typeparam>
public abstract class Trigger : Behavior, ITrigger
{
/// <summary>
/// Identifies the <seealso cref="Actions"/> dependency property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly DependencyProperty ActionsProperty = DependencyProperty.Register(
"Actions",
typeof(ActionCollection),
typeof(Trigger),
new PropertyMetadata(null));

/// <summary>
/// Gets the collection of actions associated with the behavior. This is a dependency property.
/// </summary>
public ActionCollection Actions
{
get
{
ActionCollection actionCollection = (ActionCollection)this.GetValue(Trigger.ActionsProperty);
if (actionCollection == null)
{
actionCollection = new ActionCollection();
this.SetValue(Trigger.ActionsProperty, actionCollection);
}

return actionCollection;
}
}
}
}
42 changes: 42 additions & 0 deletions src/BehaviorsSDKManaged/Microsoft.Xaml.Interactivity/TriggerOfT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Windows.UI.Xaml;

namespace Microsoft.Xaml.Interactivity
{
/// <summary>
/// A base class for behaviors, implementing the basic plumbing of ITrigger
/// </summary>
public abstract class Trigger<T> : Trigger where T : DependencyObject
{
/// <summary>
/// Gets the object to which this behavior is attached.
/// </summary>
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public new T AssociatedObject
{
get { return base.AssociatedObject as T; }
}

/// <summary>
/// Called after the behavior is attached to the <see cref="Microsoft.Xaml.Interactivity.Behavior.AssociatedObject"/>.
/// </summary>
/// <remarks>
/// Override this to hook up functionality to the <see cref="Microsoft.Xaml.Interactivity.Behavior.AssociatedObject"/>
/// </remarks>
protected override void OnAttached()
{
base.OnAttached();

if (this.AssociatedObject == null)
{
string actualType = base.AssociatedObject.GetType().FullName;
string expectedType = typeof(T).FullName;
string message = string.Format(ResourceHelper.GetString("InvalidAssociatedObjectExceptionMessage"), actualType, expectedType);
throw new InvalidOperationException(message);
}
}
}
}

0 comments on commit 971031a

Please sign in to comment.