2011-06-14 63 views
8

我有一個ComboBox。在不改變模板的情況下,當用戶將鼠標放在ComboBoxItem上,但在選擇實際發生之前,是否可以啓動代碼?看起來我應該能夠指定一個EventTrigger或一個觸發器以ComboBoxItem的風格來完成此操作。WPF:IsMouseOver ComboBoxItem時啓動代碼

<ComboBox Grid.Column="1" Grid.Row="0" 
      ItemsSource="{Binding Voices}"         
      SelectedItem="{Binding SelectedVoice, Mode=TwoWay}"> 
    <ComboBox.Resources> 
     <Style TargetType="{x:Type ComboBoxItem}"> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        ... Launch my code from code behind... but HOW? ... 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ComboBox.Resources> 
</ComboBox> 

我也行與ousing一個的MouseEnter,但如果可能的話,我寧願不建立一個獨立的DataTemplate或的ContentTemplate。

更新。這段代碼背後的想法是當用戶將鼠標懸停在新的語音上時播放測試音頻,我必須從代​​碼端進行測試。幫幫我!

+0

你如何處理了前兩項移動鼠標,同時將第三個用戶的情況下?你會播放前兩個聲音嗎? – 2011-06-14 15:41:29

+0

實際的代碼會觸發一個thread.sleep的線程,等待100-200毫秒以確保用戶仍然在同一個項目上,在這種情況下,它將檢查ismouseover是否仍然爲真 – tofutim 2011-06-14 15:50:47

+0

不是您的答案問題,但實現你的想法的兩個提示:要在某些事件上播放一些音頻,請將類一起使用。或者,使用類並處理鼠標事件。 – 2011-06-14 18:09:16

回答

4

您可以使用EventSetter

<ComboBox.Resources> 
    <Style TargetType="{x:Type ComboBoxItem}"> 
     <EventSetter Event="PreviewMouseMove" Handler="ComboBoxItem_PreviewMouseMove" /> 
    </Style> 
</ComboBox.Resources> 

在後面的代碼:

private void ComboBoxItem_PreviewMouseMove(object sender, MouseEventArgs e) 
{ 
    ComboBoxItem item = sender as ComboBoxItem; 
    //Now you can use this Item 
} 
+0

嘿,這可能工作。 – tofutim 2011-06-14 19:35:09

+0

是的,它適合我 – 2011-06-14 19:39:04

0

我知道一個骯髒的解決方案..萬一你跑出來的解決方案試試這個作爲你最後的希望..

我通過創建一個XAMLtextblock設置其text等於comboboxitemcontent一次測試此mouseover並設置text""一次mouseleft

我使用AttachedBehaviours找出哪個特定的comboboxitemmouse over一次鼠標是存在的,也收到通知,一旦鼠標不超過它了或鼠標左

試試這個..創建一個類

public static class ComboBoxBehaviour 
    { 
     //holding reference of MainWindow class to update the textBlock 
     public static MainWindow windoewRef ; 

     public static bool GetTest(ComboBoxItem target) 
     { 
      return (bool)target.GetValue(TestAttachedProperty); 
     } 

     public static void SetTest(ComboBoxItem target, bool value) 
     { 
      target.SetValue(TestAttachedProperty, value); 
     } 

     public static readonly DependencyProperty TestAttachedProperty = DependencyProperty.RegisterAttached("Test", typeof(bool), typeof(ComboBoxBehaviour), new UIPropertyMetadata(false, OnMouseOverChanged)); 

     static void OnMouseOverChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) 
     { 
      ComboBoxItem item = o as ComboBoxItem; 
      if ((bool)e.NewValue) 
      { 
       // I am setting text of a textblock for testing once mouse is over an item 
       windoewRef.textBlock.Text = item.Content.ToString(); 
      } 
      else 
      { 
       //setting text to "" once mouse has been moved 
       windoewRef.textBlock.Text = ""; 
      } 
     } 
    } 

在XAML

<TextBlock Text="" x:Name="textBlock" /> 
     <ComboBox x:Name="combo"> 

      <ComboBox.Resources> 
       <Style TargetType="{x:Type ComboBoxItem}" xmlns:behaviours="clr-namespace:WpfApplication1"> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="behaviours:ComboBoxBehaviour.Test" Value="True"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="False"> 
          <Setter Property="behaviours:ComboBoxBehaviour.Test" Value="False"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </ComboBox.Resources> 
     </ComboBox> 

我知道這是一個不好的解決方案,它可能有問題,我還沒有找到,但只是我的想法...

+0

哈哈,這很骯髒,但我很欣賞這個解決方案。 ;)當然有一種方法是重新創建內容或數據模板。 – tofutim 2011-06-14 17:35:51