[WPF] 進行Event Binding

在建立form based application 時常常都會觸發事件, 而以往都會在code-behind 處理, 然而這樣會令測試變得難以透過Unit Test Project 自動化. 亦因此在WPF 中亦有方法去應對.

加入 System.Windows.InterActivity 作reference 後, 再於需要叫用的form 加入以下xml namespace 作叫用.

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

使用範例:

<DataGrid>
<i:Interaction.Triggers>
                        <i:EventTrigger EventName="SourceUpdated">
                            <i:InvokeCommandAction Command="{Binding SelectedExternalDataUpdated}" />
                        </i:EventTrigger>
                        <i:EventTrigger EventName="SelectionChanged">
                            <i:InvokeCommandAction Command="{Binding SelectedExternalDataChanged}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                <DataGrid>

 

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

1 Trackback / Pingback

  1. [WPF] 在ViewModel 中實現Command Binding – Ling's Note

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.