WPF中的Trigger一般应用于Style、ControlTemplate、DataTemplate这三个地方,主要用于改变控件颜色、形状、显示等样式。
WPF中trigger的主要类型有:Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种。
但是在实际应用过程中,我们可能会遇到一些复杂的逻辑,需要自定义一些处理事件,那么这时候Trigger就显得捉襟见肘,难以处理。
这是时候我们可以用Behavior、TriggerAction 、TargetedTriggerAction来处理。
//自定义处理触发事件
namespace TriggerAction
{
public class MouseEnterBehavior: Behavior< TextBlock>
{
protected override void OnAttached()
{
this.AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
this.AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
}
void AssociatedObject_MouseLeave( object sender, System.Windows.Input.MouseEventArgs e)
{
this.AssociatedObject.Background = Brushes.Transparent;
}
void AssociatedObject_MouseEnter( object sender, System.Windows.Input.MouseEventArgs e)
{
this.AssociatedObject.Background = Brushes.Aquamarine;
}
protected override void OnDetaching()
{
this.AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
this.AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
}
}
public class MouseEnterAction : TriggerAction<TextBlock >
{
protected override void Invoke( object parameter)
{
this.AssociatedObject.Text = "MouseEnterAction";
}
}
public class TargetMouseEnterAction : TargetedTriggerAction<TextBlock >
{
protected override void Invoke( object parameter)
{
this.Target.Text = ( this.AssociatedObject as TextBlock).Text;
this.Target.Background = ( this.AssociatedObject as TextBlock).Background;
}
}
public class OpenWindowAction : TriggerAction<DependencyObject>
{
public Type WindowType
{
get { return (Type)GetValue(WindowTypeProperty); }
set { SetValue(WindowTypeProperty, value); }
}
public static readonly DependencyProperty WindowTypeProperty =
DependencyProperty.Register("WindowType", typeof(Type), typeof(OpenWindowAction), new PropertyMetadata(null));
protected override void Invoke(object parameter)
{
var windowObj= Activator.CreateInstance(WindowType) as Window;
windowObj.ShowDialog();
}
}
public class ButtonAction : TriggerAction<Button>
{
protected override void Invoke(object parameter)
{
this.AssociatedObject.Content = DateTime.Now.ToString();
}
}
public class ButtonTargetAction : TargetedTriggerAction<Button>
{
protected override void Invoke(object parameter)
{
this.Target.Content = this.Target.Content + "1";
}
}
}
前端代码
<Window x:Class="TriggerAction.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:TriggerAction"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="331,39,0,0" VerticalAlignment="Top" Width="164" Height="46">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<local:OpenWindowAction WindowType="{x:Type local:Window1}" />
<local:ButtonAction />
<local:ButtonTargetAction TargetName="aaa" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="aaa" Content="Button" HorizontalAlignment="Left" Margin="331,71,0,0" VerticalAlignment="Top" Width="103" Height="30"/>
<TextBlock HorizontalAlignment ="Left" Height="17" Margin="32,39,0,0" TextWrapping="Wrap" Text="MouseEnterBehavior" VerticalAlignment ="Top" Width="124">
<i: Interaction.Behaviors>
<behaviorSample: MouseEnterBehavior/>
</i: Interaction.Behaviors>
<i: Interaction.Triggers>
<i: EventTrigger EventName="MouseDown">
<behaviorSample: MouseEnterAction/>
<behaviorSample: TargetMouseEnterAction TargetName="AssociateTb"/>
</i: EventTrigger>
</i: Interaction.Triggers>
</TextBlock>
<TextBlock x :Name="AssociateTb" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TargetTb" VerticalAlignment ="Top" Margin="32,71,0,0" Width="113"/>
</Grid>
</Window>