網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Interaction Class?- static class that owns the Triggers and Behaviors attached properties. Handles propagation of AssociatedObject change notifications (MSDN).
當(dāng)不足以使用ICommand的時(shí)候,這種特殊的手段對(duì)MVVM模式非常有用。
我們需要在我們的項(xiàng)目中添加兩個(gè)引用:
-?Microsoft.Expression.Interactions.dll
-?System.Windows.Interactivity.dll
代碼例子:
- 1)引用Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll
在UserControl添加兩個(gè)特性
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>
如果我們想在這個(gè)方法中使用參數(shù),我們應(yīng)該在ViewModel中使用屬性(例如雙向綁定)。
附錄:使用ICommand的例子。
//區(qū)別:這樣可以傳參數(shù),但是綁定的是實(shí)現(xiàn)了ICommand接口的類的對(duì)象。
//步驟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>
原文鏈接:https://www.cnblogs.com/lynn-/p/3262658.html
相關(guān)推薦
- 2023-05-23 Numpy數(shù)組轉(zhuǎn)置的實(shí)現(xiàn)_python
- 2022-07-08 Python如何讀取csv文件時(shí)添加表頭/列名_python
- 2023-06-17 python?__init__與?__new__的區(qū)別_python
- 2022-05-23 Docker容器鏡像相關(guān)命令基本介紹與使用_docker
- 2022-09-24 C語(yǔ)言利用面試真題理解指針的使用_C 語(yǔ)言
- 2022-01-13 出現(xiàn)/usr/lib64/erlang/erts-10.3/bin/beam.smp: error
- 2022-09-08 Go語(yǔ)言中make和new函數(shù)的用法與區(qū)別_Golang
- 2022-10-18 docker-desktop啟動(dòng)k8s的詳細(xì)方法_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支