2014-10-31 19 views
0

我想在按下按鈕時更改TextBlock,然後在釋放按鈕時返回到之前的狀態。對Button.IsPressed的響應WPF

似乎RepeatButton不是這裏的解決方案,因爲它只對自己被持有而不被釋放做出反應 - 而且我需要知道它何時被釋放,以便我可以運行正確的方法將TextBlock返回到其原始狀態。絕望,我也嘗試循環while(button.IsPressed)(是的,我知道,可怕的主意:()但無濟於事 - 代碼將掛起(就像按鈕被釋放後IsPressed沒有改變爲假)

是有什麼辦法來實現它在此先感謝

+0

最佳的解決方案,它是否適合您的方案,就是使用觸發器和setter方法,無論是通過樣式或模板應用。 – 2014-10-31 20:59:31

+0

在我看來,這太複雜了,因爲我認爲這是常見的問題。我會看看這裏可以做些什麼。謝謝 – 3yakuya 2014-10-31 21:20:03

+0

'PreviewMouseDown','PreviewMouseUp' ...使用這些事件來啓動/停止進程。 – 2014-11-01 22:34:27

回答

0

也許不是最乾淨的方式,但我決定創建多個處理程序,以我的按鈕:?。ClickPointerPressedPointerCancelledPointerCaptureLostPointerReleased前兩個是用於處理該按鈕是按下,而最後三個是用於處理釋放。我使用全部三個由於推薦在這裏:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.pointerreleased

這是因爲PointerReleased有時可能被按鈕釋放時觸發的其他事件替代。

0

PreviewMouseDown和PreviewMouseUp似乎做工精細,如果你想左,右點擊有你想要的效果:

public partial class MainWindow : Window 
{ 
    private string TextBlockPreviousState = ""; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     ButtonStatusTextBlock.Text = "foo"; 
    } 

    private void StoreAndUpdate() 
    { 
     TextBlockPreviousState = ButtonStatusTextBlock.Text; 
     ButtonStatusTextBlock.Text = "Button Down"; 
    } 

    private void Restore() 
    { 
     ButtonStatusTextBlock.Text = TextBlockPreviousState; 
    } 

    private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e) 
    { 
     StoreAndUpdate(); 
    } 

    private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e) 
    { 
     Restore(); 
    } 
}