Last active
January 1, 2016 11:09
-
-
Save KatsuYuzu/8135825 to your computer and use it in GitHub Desktop.
Windows ストアアプリの独自のTriggerBehaviorサンプル。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xaml.Interactivity; | |
using System; | |
using Windows.ApplicationModel; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Markup; | |
namespace App1 | |
{ | |
[ContentProperty(Name = "Actions")] | |
public class MyTriggerBehavior : DependencyObject, IBehavior | |
{ | |
public DependencyObject AssociatedObject | |
{ | |
get { return this.associatedObject; } | |
} | |
private DependencyObject associatedObject; | |
public void Attach(DependencyObject associatedObject) | |
{ | |
if (associatedObject == this.associatedObject || DesignMode.DesignModeEnabled) | |
{ | |
return; | |
} | |
if (this.associatedObject != null) | |
{ | |
throw new InvalidOperationException(); | |
} | |
this.associatedObject = associatedObject; | |
RegisterTrigger(); | |
} | |
public void Detach() | |
{ | |
UnRegisterTrigger(); | |
this.associatedObject = null; | |
} | |
public ActionCollection Actions | |
{ | |
get | |
{ | |
var actionCollection = (ActionCollection)GetValue(ActionsProperty); | |
if (actionCollection == null) | |
{ | |
actionCollection = new ActionCollection(); | |
SetValue(MyTriggerBehavior.ActionsProperty, actionCollection); | |
} | |
return actionCollection; | |
} | |
} | |
public static readonly DependencyProperty ActionsProperty = | |
DependencyProperty.Register("Actions", typeof(ActionCollection), typeof(MyTriggerBehavior), | |
new PropertyMetadata(null)); | |
private void RegisterTrigger() | |
{ | |
var button = this.associatedObject as Button; | |
if (button == null) | |
{ | |
return; | |
} | |
button.Click += OnClick; | |
} | |
private void UnRegisterTrigger() | |
{ | |
var button = this.associatedObject as Button; | |
if (button == null) | |
{ | |
return; | |
} | |
button.Click -= OnClick; | |
} | |
private void OnClick(object sender, RoutedEventArgs args) | |
{ | |
Interaction.ExecuteActions(sender, this.Actions, args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment