亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WPF 按鈕樣式觸發器觸發兩次

WPF 按鈕樣式觸發器觸發兩次

C#
開滿天機 2022-10-15 15:15:52
我目前正在開發一個 .NET 4.7.1 WPF 應用程序。我在 IsPressed 處理程序的按鈕樣式上有附加行為。我可以到達事件處理程序。但是,當我單擊按鈕時,不幸的是,該事件以某種方式被觸發了兩次。我的 xml 看起來像這樣:<WrapPanel Orientation="Horizontal" HorizontalAlignment="Right">    <Button Style="{StaticResource MySaveButton}">        Save    </Button></WrapPanel>我的風格是這樣的:<Style TargetType="Button" BasedOn="{StaticResource SomeOtherBaseStyle}" x:Key="MySaveButton">    <Style.Triggers>        <Trigger Property="IsPressed" Value="True">            <Setter Property="localBehaviors:MyBehavior.Save" Value="True" />        </Trigger>    </Style.Triggers></Style>我的行為類如下所示: public class MyBehavior : Behavior<UIElement> {     public static readonly DependencyProperty SaveProperty =         DependencyProperty.RegisterAttached("Save", typeof(bool), typeof(MyBehavior), new UIPropertyMetadata(OnSave));     public static bool GetSave(DependencyObject obj) => (bool)obj.GetValue(SaveProperty);     public static void SetSave(DependencyObject obj, bool value) => obj.SetValue(SaveProperty, value);     private static void OnSave(DependencyObject d, DependencyPropertyChangedEventArgs e)     {        // gets triggered twice when I click the button. Should be raised only once.        // ... some logic     }}你知道如何使用樣式觸發器只觸發一次事件嗎?
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

當值發生IsPressed變化(從true到false或相反)時,觸發器正在執行。這意味著它在按下和釋放按鈕時被調用。


要檢查哪個方向導致觸發,您可以檢查e.NewValue:


private static void OnSave(DependencyObject d, DependencyPropertyChangedEventArgs e)

{

    if (e.NewValue)

    {

        // changed from unpressed to pressed

    }

}


查看完整回答
反對 回復 2022-10-15
  • 1 回答
  • 0 關注
  • 174 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號