Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.