WPF-Interaction.Triggers
?
??? 本次所分享的是Interaction.Triggers,Interaction.Triggers到底是什么呢?有什么用?Interaction.Triggers其實是WPF中的一種觸發器,其作用是交互,所以它又叫交互觸發器。
??? 其實一般情況下用到這種特殊的交互觸發器,在不太需要的情況下是不會去使用。那究竟在什么時候使用呢?當WPFmvvm模式下某個控件或其他:好比如Button的點擊事件,在mvvm模式下Button的點擊事件有時候是不可以直接使用ICommand的,這時候這中特殊手段就對mvvm模式非常有用,但是在使用Interaction.Triggers之前我們的項目還需要添加一個引用:
System.Windows.Interactivity.dll
接下來直接進入步驟:
以Button舉個例子:
首先引用System.Window.Interactivity.dll
在您的xaml(UserControl或者是Window)添加這個特性
<UserControl x:Class="……………"
??????????? ?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??????????? ?xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??????????? ?xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
?????????? ?Height="500" Width="800">
或者:
<UserControl x:Class="Afterall_MVVM.Views.TPIstorageManagement"
??????????? ?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
??????????? ?xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
??????????? ?xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
?????????? ?Height="500" Width="800">
?
接下來就在需要的地方,比如某個不小心的Button
<Button Height="18" Width="18" Margin="5 0 0 0">
??????????????????????????????????????? <i:Interaction.Triggers>
??????????????????????????????????????????? <i:EventTrigger EventName="Click">
??????????????????????????????????????????????? <i:InvokeCommandAction Command="{Binding tvSupplierCommand}"/>
??????????????????????????????????????????? </i:EventTrigger>
??????????????????????????????????????? </i:Interaction.Triggers>
??????????????????????????????????? </Button>
?
這里EventName中是放想要執行的事件,好比如想要個雙擊事件MouseDoubleClick,也可以是Click。
然后再ViewModel中創建pulic方法
public void TPIstorageInit()
??????? {
??????????? //打開供應商
??????????? tvSupplierCommand = new RelayCommand(tvSupplier);
??????? }
public RelayCommand tvSupplierCommand { get; set; }
//打開選項數據表格
??????? public void tvSupplier()
??????? {
??????????? SupplierAddWindow my = new SupplierAddWindow();
??????????? var myViewModel = (my.DataContext as SupplierAddViewModes);
??????????? my.ShowDialog();
??????? }
?
附圖:
?
?
?
?
總結
以上是生活随笔為你收集整理的WPF-Interaction.Triggers的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flink触发器Triggers
- 下一篇: zabbix探究告警触发器Trigger