在WPF中使用Interaction.Triggers
Interaction Class - static class that owns the Triggers and Behaviors attached properties. Handles propagation of AssociatedObject change notifications (MSDN).
當(dāng)不足以使用ICommand的時(shí)候,這種特殊的手段對(duì)MVVM模式非常有用。
我們需要在我們的項(xiàng)目中添加兩個(gè)引用:
- Microsoft.Expression.Interactions.dll
- System.Windows.Interactivity.dll
代碼例子:
- 1)引用Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll
在UserControl添加兩個(gè)特性
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" <!--或者--> xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
- 2) 在ViewModel中創(chuàng)建public方法
public void SubmitClicked() { MessageBox.Show("Button was clicked"); }
- 3) 在UserControl中添加button到Xaml
<Button Content="Submit Method" Width="180"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <ei:CallMethodAction TargetObject="{Binding}" MethodName="SubmitClicked"/> </i:EventTrigger> </i:Interaction.Triggers> </Button>
如果我們想在這個(gè)方法中使用參數(shù),我們應(yīng)該在ViewModel中使用屬性(例如雙向綁定)。
附錄:使用ICommand的例子。
//區(qū)別:這樣可以傳參數(shù),但是綁定的是實(shí)現(xiàn)了ICommand接口的類的對(duì)象。 //步驟1:引用System.Windows.Interactivity.dll,添加特性。 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" //步驟2:使用?!? <i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <i:InvokeCommandAction Command="{Binding ClockWindowLoadCommand}" CommandParameter="{Binding ElementName=txtCardNo}"/> </i:EventTrigger> <i:EventTrigger EventName="KeyUp"> <i:InvokeCommandAction Command="{Binding WindowKeyCommand}"/> </i:EventTrigger> <i:EventTrigger EventName="Activated"> <i:InvokeCommandAction Command="{Binding WindowActivatedCommand}" CommandParameter="{Binding ElementName=txtCardNo}"/> </i:EventTrigger> </i:Interaction.Triggers>
到此這篇關(guān)于在WPF中使用Interaction.Triggers的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
DevExpress之餅狀圖突出(Explode)設(shè)置實(shí)例
這篇文章主要介紹了DevExpress之餅狀圖突出(Explode)設(shè)置方法,以實(shí)例形式展示了餅狀圖突出設(shè)置的具體實(shí)現(xiàn)過(guò)程,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10C#實(shí)現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實(shí)例教程
這篇文章主要介紹了C#實(shí)現(xiàn)Stream與byte[]之間的轉(zhuǎn)換方法,具體講解了二進(jìn)制轉(zhuǎn)換成圖片、byte[]與string的轉(zhuǎn)換、Stream 和 byte[] 之間的轉(zhuǎn)換、Stream 和 文件之間的轉(zhuǎn)換、從文件讀取 Stream以及Bitmap 轉(zhuǎn)化為 Byte[]等,需要的朋友可以參考下2014-09-09基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼
在微信小程序管理后臺(tái),我們可以生成下載標(biāo)準(zhǔn)的小程序二維碼,提供主程序入口功能,在實(shí)際應(yīng)用開(kāi)發(fā)中,小程序二維碼是可以攜帶參數(shù)的,可以動(dòng)態(tài)進(jìn)行生成,本文小編就給大家介紹一下如何基于C#動(dòng)態(tài)生成帶參數(shù)的小程序二維碼,感興趣的朋友可以參考下2023-12-12C#連接SQL?Sever數(shù)據(jù)庫(kù)與數(shù)據(jù)查詢實(shí)例之?dāng)?shù)據(jù)倉(cāng)庫(kù)詳解
最近的工作遇到了連接查詢,特在此記錄,以免日后以往,下面這篇文章主要給大家介紹了關(guān)于C#連接SQL?Sever數(shù)據(jù)庫(kù)與數(shù)據(jù)查詢實(shí)例之?dāng)?shù)據(jù)倉(cāng)庫(kù)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06C#用表達(dá)式樹(shù)構(gòu)建動(dòng)態(tài)查詢的方法
這篇文章主要介紹了C#用表達(dá)式樹(shù)構(gòu)建動(dòng)態(tài)查詢的方法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-12-12