網(wǎng)站制作那家便宜網(wǎng)絡(luò)營(yíng)銷有哪些推廣方式
背景:實(shí)現(xiàn)按鈕鼠標(biāo)移動(dòng)到上方有點(diǎn)交互效果或變一下有陰影。這樣使用觸發(fā)器就行了,但是如果是每個(gè)控件都有效果的話使用行為更加合適
1、下載NuGet包:Microsoft.xaml.behavior.wpf
2、創(chuàng)建行為類EffectBehavior,對(duì)Behavior進(jìn)行重寫(xiě)
public class EffectBehavior : Behavior<FrameworkElement>
{protected override void OnAttached(){base.OnAttached();// 這個(gè)時(shí)候的AssociatedObject就是FrameworkElement,因?yàn)榉盒蛡鬟^(guò)去了AssociatedObject.MouseMove += AssociatedObject_MouseMove; // 鼠標(biāo)進(jìn)入AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;}private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e){var element = sender as FrameworkElement;// 設(shè)置效果element.Effect = (Effect)new DropShadowEffect() { Color = Colors.Transparent, ShadowDepth = 0 };}private void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e){var element = sender as FrameworkElement;element.Effect = (Effect)new DropShadowEffect() { Color = Colors.Red, ShadowDepth = 0 };}protected override void OnDetaching(){base.OnDetaching();AssociatedObject.MouseMove -= AssociatedObject_MouseMove; // 鼠標(biāo)進(jìn)入AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;}
}
? ? ? ? -- 就是簡(jiǎn)單加上鼠標(biāo)移動(dòng)到控件上面加上陰影效果
? ? ? ? -- 抽象類Behavior的泛型傳入的是FrameworkElement是因?yàn)?#xff0c;大多數(shù)控件都是由它派生出來(lái)的,具體可以查看這個(gè)文章的WPF控件結(jié)構(gòu):https://www.cnblogs.com/zh7791/p/11372473.html
3、在xaml中引入NuGet的命名空間
4、將自己重寫(xiě)的behavior給控件使用
<StackPanel><TextBox Width="100" Height="30" Margin="40"><i:Interaction.Behaviors><local:EffectBehavior/></i:Interaction.Behaviors></TextBox><Button Width="100" Height="30" Margin="40"><i:Interaction.Behaviors><local:EffectBehavior/></i:Interaction.Behaviors></Button>
</StackPanel>
總結(jié):對(duì)Behavior進(jìn)行重寫(xiě)罷了
同樣也是這個(gè)NuGet的使用
WPF實(shí)現(xiàn)更加靈活綁定復(fù)雜Command(使用Microsoft XAML Behaviors 庫(kù))_wpf 綁定復(fù)雜類型-CSDN博客