Skip to content

Instantly share code, notes, and snippets.

@KatsuYuzu
Last active January 1, 2016 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KatsuYuzu/8135825 to your computer and use it in GitHub Desktop.
Save KatsuYuzu/8135825 to your computer and use it in GitHub Desktop.
Windows ストアアプリの独自のTriggerBehaviorサンプル。
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