
在建立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>
Leave a Reply