搜尋此網誌

2009年12月15日 星期二

Silverlight:讓Button能夠使用 MouseLeftButtonDown & Up

Silverlight在使用Button時,只有「Click」事件能夠產生動作,而「MouseLeftButtonDown」及「MouseLeftButtonUp」…等多個Mouse動作都無作用。
在某些狀況下我們可能需要「MouseDown」及「MouseUp」配合使用,針對這項需求,可使用以下方式解決--

在此以RepeatButton舉例:
對xmal檔中的button物件指定「Click」、「MouseLeftButtonDown」、「MouseLeftButtonUp」等事件,並設定「ClickMode」為「Hover」:

<RepeatButton  Opacity="0.01" Width="24" Height="26" Interval="100"
  Margin="-8" ClickMode="Hover"
  Click="on_tomorrow" MouseLeftButtonDown="on_tomorrow_down"
  MouseLeftButtonUp="on_tomorrow_up"/>

在cs檔中寫前對應的function。將「ClickMode」設定為「Hover」時,只要mouse置於button上就會觸發Click事件,mouse置於button上時,以一個bool變數去判斷mouse是不是Down的狀態,即可使用MouseLeftButtonDown及MouseLeftButtonUp的功能:

 bool butmDown = false;
 private void on_tomorrow(object sender, RoutedEventArgs e)
 {
  if (!butmDown) return;
  …//when mouse down, do something on this code..
  …
 }
 private void on_tomorrow_down(object sender, MouseButtonEventArgs e)
 {
  butmDown = true;
 }
 private void on_tomorrow_up(object sender, MouseButtonEventArgs e)
 {
  MessageBox.Show("release mouse");
  butmDown = false;
 }

沒有留言:

張貼留言