編輯:簡而言之:正如 AFigmentOfMyImagination 在他的回答中指出的那樣,不存在任何IsPressedChanged事件。但是,這是原始問題:
我有一個按鈕,我希望在按下按鈕和釋放按鈕時都執行命令。按鈕的標準命令系結只能在按下或釋放按鈕時呼叫命令,所以我想我可以使用IsPressedChanged事件上的互動觸發器來實作我想要的,如下所示:
<Button xmlns:i="http://schemas.microsoft.com/xaml/behaviors">
<i:Interaction.Triggers>
<i:EventTrigger EventName="IsPressedChanged">
<i:InvokeCommandAction Command="{Binding Path=SomeCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
但這不起作用。但是,當我使用其他事件(如MouseEnter事件)時,它確實有效。
現在我的問題是,為什么會這樣?IsPressedChanged該事件與其他事件有什么區別?在我看來,好像存在一些概念上的差異,我想知道那是什么(也許為什么需要有所不同)。
編輯:我找到了一種在每次IsPressed更改時都執行命令的方法。該解決方案用于此問題。
uj5u.com熱心網友回復:
和之間的區別在于IsPressedChanged,MouseEvent它MouseEvent是由 while 提供的事件,Button而IsPressedChanged不是。
換句話說,不存在的東西不能真正“作業”......
uj5u.com熱心網友回復:
EventTrigger用于路由事件。
沒有IsPressedChanged路由事件,a 提供的唯一路由事件Button是Click,它不符合您的需求。
在全域路由事件Mouse.MouseDown中,您可以找到Mouse.MouseUp但您會錯過通過鍵盤按下按鈕的情況。
您可以Button.IsPressed通過資料觸發器使用該屬性,并將系結源設定為按鈕(通過使用ElementName或RelativeSource)。
但是您將不得不稍微調整一下,以便在值為true或時觸發false。
幸運的是,它們都不等于true或false不等于空字串。通過設定DataTrigger.Comparisonto"NotEqual"和Valueto""確保每次IsPressed更改時觸發觸發器。
(可能的不良影響:它會在控制元件創建后立即觸發。)
<Button x:Name="Button">
<i:Interaction.Triggers>
<!--
You can also use this binding
<i:DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button},
Path=IsPressed}"
-->
<i:DataTrigger Binding="{Binding ElementName=Button, Path=IsPressed}"
Comparison="NotEqual"
Value="">
<i:InvokeCommandAction Command="{Binding Path=PressedChangedCommand}"/>
</i:DataTrigger>
</i:Interaction.Triggers>
</Button>
可在此處獲得作業演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475807.html
