1. 程式人生 > >【WPF/WAF】使用System.Windows.Interactivity互動事件

【WPF/WAF】使用System.Windows.Interactivity互動事件

原文: 【WPF/WAF】使用System.Windows.Interactivity互動事件

下載System.Windows.Interactivity.dll檔案,並引入專案中(在VS專案的引用列表中可以看到)。可在Nuget搜尋System.Windows.Interactivity下載安裝到專案中。

XAML中使用該dll

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<!-- TextBox控制元件的獲得焦點、失去焦點事件 -->
<TextBox Text="Test"> <i:Interaction.Triggers> <i:EventTrigger EventName="LostFocus"> <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.OnTextLostFocus}" CommandParameter
="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type TextBox}}}"/> </i:EventTrigger> <i:EventTrigger EventName="GotFocus"> <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.OnTextGotFocus}
" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type TextBox}}}"/> </i:EventTrigger> </i:Interaction.Triggers> </TextBox>

 

由於VS中不支援System.Windows.Interactivity的智慧提示,經常要查閱這個EventTrigger還能觸發哪些方法,所有EventName的列表如下: