Expression Blendã«ãã便å©æ©è½ããã¤ãã¢(Behavior)(Visual Studioからも使えます)ã§ãããããã¤ã¯èªåã§è²ã
ä½ããã¨ãåºæ¥ã¾ãã
ããã¤ãã¢ã¨ãã£ã¦ããç´ç²ãªããã¤ãã¢ã¨ãã¢ã¯ã·ã§ã³ã¨å¼ã°ããããªã¬ã¼ã¨ã»ããã§ä½¿ããã®ãããã¾ãã
ããã¤ãã¢ã¯ãå人çãªè§£éã ã¨ã³ã¼ãããã¤ã³ãã«ã¤ãã³ããã³ãã©ãæ¸ããã¨ãªãä½ãã¢ã¯ã·ã§ã³ããããããã®é¨åã¨æã£ã¦ãã¾ããããã¤ãã¢ã¨ã¢ã¯ã·ã§ã³ã®éãã§ãããã¯ãªãã¯ããã¨ãã«ã¢ã©ã¼ããåºãã¨ãã£ãããã«ãã¤ãã³ããåºå®ããã¦ãã¦ãããã«å¯¾ãã¦å¦çãè¡ããã®ãããã¤ãã¢ã§ãã¯ãªãã¯ããã¨ãã®é¨åãââã®ã¤ãã³ããã¯ã£ããããã¨ãã«ã¢ã©ã¼ããåºãã¨ãã£ãããã«ã¤ãã³ããå¥ã«ã¯ãªãã¯ãããªãã¦ãä½ã§ãããã¨ãã£ãã¨ãã«ã¢ã¯ã·ã§ã³ã使ãã¾ãã
ã¨ãããã¨ã§ããã£ããä½ãæ¹ãè¦ã¦ã¿ã¾ããã¾ãã¯ãç°¡åãªããã¤ãã¢ããã§ããããã¤ãã¢ã¯ãSystem.Windows.Interactivity.Behavior
ã¨ããããã§ããã£ãããã¿ã³ãã¯ãªãã¯ãããæå®ãããã¡ãã»ã¼ã¸ãåºåããããã¤ãã¢ãä½ã£ã¦ã¿ããã¨æãã¾ããã³ã¼ãã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
namespace WpfApplication5 { using System; using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; // Buttonã«å¯¾ãã¦é©ç¨å¯è½ãªå ´åã¯<>ã«Buttonã¨æå®ãã public class AlertBehavior : Behavior<Button> { #region ã¡ãã»ã¼ã¸ãããã㣠public string Message { get { return (string)GetValue(MessageProperty); } set { SetValue(MessageProperty, value); } } // Using a DependencyProperty as the backing store for Message. This enables animation, styling, binding, etc... public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(AlertBehavior), new UIPropertyMetadata(null)); #endregion public AlertBehavior() { } // è¦ç´ ã«ã¢ã¿ãããããã¨ãã®å¦çã大ä½ã¤ãã³ããã³ãã©ã®ç»é²å¦çãããã§ãã protected override void OnAttached() { base.OnAttached(); this.AssociatedObject.Click += Alert; } // è¦ç´ ã«ãã¿ãããããã¨ãã®å¦çã大ä½ã¤ãã³ããã³ãã©ã®ç»é²è§£é¤ãããã§ãã protected override void OnDetaching() { base.OnDetaching(); this.AssociatedObject.Click -= Alert; } // ã¡ãã»ã¼ã¸ãå ¥åããã¦ãããã¡ãã»ã¼ã¸ããã¯ã¹ãåºã private void Alert(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.Message)) { return; } MessageBox.Show(this.Message); } } }
ä¸è¬çã«ããã¤ãã¢ãä½ãã¨ãªãã¨Behaviorã¯ã©ã¹ã®OnAttachedã§ãã¤ãã³ããã³ãã©ã®ç»é²ãè¡ããOnDetachingã§ã¤ãã³ããã³ãã©ã®ç»é²è§£é¤ãè¡ãã¾ããããã¦ãã¤ãã³ããã³ãã©å
ã§ä»»æã®å¦çãè¡ãã¾ãã
ããã¤ãã¢ã«ã¯ããããã£ãä½ããã¨ãã§ãã¦ããããä¾åããããã£ã§ä½ã£ã¦ããã¨ãã¤ã³ãã¨ããã§ããããã«ãªãã¾ãã
é©å½ãªããã¸ã§ã¯ããä½ã£ã¦ãã¿ã³ãç»é¢ã«ç½®ãã¦ä»ä½ã£ãããã¤ãã¢ãã¢ã»ãããããã©ãã°ãã¦é©ç¨ãã¾ãããªãã¸ã§ã¯ãã¨ã¿ã¤ã ã©ã¤ã³ã¦ã£ã³ãã¦ã®è¡¨ç¤ºã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
ããã¦ãããããã£ã¦ã£ã³ãã¦ã§Messageããããã£ã«é©å½ãªã¡ãã»ã¼ã¸ãè¨å®ãã¾ãã
è¨å®ãçµãã£ãå¾ã®XAMLã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:WpfApplication5" x:Class="WpfApplication5.MainWindow" x:Name="Window" Title="MainWindow" Width="640" Height="480"> <Grid x:Name="LayoutRoot"> <Button Content="Button" HorizontalAlignment="Left" Margin="8,8,0,0" VerticalAlignment="Top" Width="75"> <i:Interaction.Behaviors> <local:AlertBehavior Message="ããã«ã¡ã¯ä¸ç"/> </i:Interaction.Behaviors> </Button> </Grid> </Window>
å®è¡ãã¦ãã¿ã³ãæ¼ãã¨ãã³ã¼ãããã¤ã³ãã«ã³ã¼ããæ¸ãã¦ãªãã®ã«ãã¡ãã»ã¼ã¸ããã¯ã¹ã表示ããã¾ãã
次ã¯ãã¢ã¯ã·ã§ã³ã®ä½ãæ¹ã§ããã¢ã¯ã·ã§ã³ã«ã¯ãã¿ã¼ã²ãããæå®ã§ãããã®ã¨ãã¿ã¼ã²ãããé©ç¨ããã親ãªãã¸ã§ã¯ãã«ãªãï¼ç¨®é¡ãããã¾ããããã«ãã¢ã¯ã·ã§ã³ã¯ãBehavior
namespace WpfApplication5 { using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; // // Action ã®ã¿ã¼ã²ããè¦ç´ ããã®è¦ªä»¥å¤ã«ãããå ´åãã¯ã©ã¹ã // TriggerAction ã®ä»£ããã« TargetedTriggerAction ããæ¡å¼µãã¾ã // public class AlertAction : TriggerAction<Button> { #region Messageãããã㣠public string Message { get { return (string)GetValue(MessageProperty); } set { SetValue(MessageProperty, value); } } public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(AlertAction), new UIPropertyMetadata(null)); #endregion public AlertAction() { } // Actionãå®è¡ãããã¨ãã®å¦ç protected override void Invoke(object o) { if (string.IsNullOrEmpty(this.Message)) { return; } MessageBox.Show(this.Message); } } }
Invokeã¡ã½ããã§ããããå¦çãæ¸ãã ãã§OKãªã®ã§ãBehaviorãç¶æ¿ããã¨ããããã·ã³ãã«ã«ãªã£ã¦ãã¾ããã¢ã¯ã·ã§ã³ã®Invokeã¡ã½ããå ã§ç¹å®ã®ã¿ã¼ã²ãããªããã«å¯¾ãã¦å¦çããããå ´åã¯ãã¯ã©ã¹ã®ã³ã¡ã³ãã«ããããã«TargetedTriggerActionãç¶æ¿ãã¦ãInvokeã¡ã½ããå ã§this.TargetObjectã®ããã«ãã¦ã¢ã¯ã»ã¹ãããã¨ãåºæ¥ã¾ãã
ãã¦ããã®ã¢ã¯ã·ã§ã³ã使ã£ã¦ã¿ã¾ããç»é¢ã«é©å½ã«ãã¿ã³ãç½®ãã¦ããã®ã¢ã¯ã·ã§ã³ããããããã¾ãããªãã¸ã§ã¯ãã¨ã¿ã¤ã ã©ã¤ã³ã¦ã£ã³ãã¦ã§ã¯ä»¥ä¸ã®ãããªè¡¨ç¤ºã«ãªãã¾ãã
ããã¦ãããããã£ã¦ã£ã³ãã¦ã§ã¢ã¯ã·ã§ã³ãå®è¡ããTriggerã®è¨å®ã¨ããããã£ã®è¨å®ãè¡ãã¾ãã
ä¸ã®å³ã§ã¯ããã¿ã³ã®Clickã¤ãã³ããããªã¬ã¼ã«ãã¦ã¢ã¯ã·ã§ã³ãå®è¡ããããã«è¨å®ãã¦ãã¾ããããã§ãã¤ãã³ããã¯ãªãã¯ä»¥å¤ã«ãããã¨ãã§ãã¾ãã
XAMLã§ã¯ã以ä¸ã®ããã«ãªãã¾ãã
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:WpfApplication5" x:Class="WpfApplication5.MainWindow" x:Name="Window" Title="MainWindow" Width="640" Height="480"> <Grid x:Name="LayoutRoot"> <Button Content="Button" HorizontalAlignment="Left" Margin="8,8,0,0" VerticalAlignment="Top" Width="75"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <local:AlertAction Message="ããã«ã¡ã¯ã¢ã¯ã·ã§ã³"/> </i:EventTrigger> </i:Interaction.Triggers> </Button> </Grid> </Window>
å®è¡ãã¦ãã¿ã³ãã¯ãªãã¯ããã¨ãã¡ãã»ã¼ã¸ããã¯ã¹ã表示ããã¾ãã
ãã®ããã«ãããã¤ãã¢ãç°¡åã«ä½ã£ã¦ã³ã³ããã¼ã«ã«é©ç¨ãããã¨ãåºæ¥ã¾ããããã¤ã¯ä¾¿å©ã§ãã