在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的時候,這種特殊的手段對MVVM模式非常有用。
我們需要在我們的項目中添加兩個引用:
- Microsoft.Expression.Interactions.dll
- System.Windows.Interactivity.dll
代碼例子:
- 1)引用Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll
在UserControl添加兩個特性
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>
如果我們想在這個方法中使用參數(shù),我們應(yīng)該在ViewModel中使用屬性(例如雙向綁定)。
附錄:使用ICommand的例子。
//區(qū)別:這樣可以傳參數(shù),但是綁定的是實現(xiàn)了ICommand接口的類的對象。 //步驟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的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
DevExpress之餅狀圖突出(Explode)設(shè)置實例
這篇文章主要介紹了DevExpress之餅狀圖突出(Explode)設(shè)置方法,以實例形式展示了餅狀圖突出設(shè)置的具體實現(xiàn)過程,非常具有實用價值,需要的朋友可以參考下2014-10-10C#實現(xiàn)Stream與byte[]之間的轉(zhuǎn)換實例教程
這篇文章主要介紹了C#實現(xiàn)Stream與byte[]之間的轉(zhuǎn)換方法,具體講解了二進制轉(zhuǎn)換成圖片、byte[]與string的轉(zhuǎn)換、Stream 和 byte[] 之間的轉(zhuǎn)換、Stream 和 文件之間的轉(zhuǎn)換、從文件讀取 Stream以及Bitmap 轉(zhuǎn)化為 Byte[]等,需要的朋友可以參考下2014-09-09C#連接SQL?Sever數(shù)據(jù)庫與數(shù)據(jù)查詢實例之?dāng)?shù)據(jù)倉庫詳解
最近的工作遇到了連接查詢,特在此記錄,以免日后以往,下面這篇文章主要給大家介紹了關(guān)于C#連接SQL?Sever數(shù)據(jù)庫與數(shù)據(jù)查詢實例之?dāng)?shù)據(jù)倉庫的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-06-06