我知道一個骯髒的解決方案..萬一你跑出來的解決方案試試這個作爲你最後的希望..
我通過創建一個XAML
和textblock
設置其text
等於comboboxitem
content
一次測試此mouse
是over
並設置text
到""
一次mouse
有left
我使用AttachedBehaviours
找出哪個特定的comboboxitem
是mouse 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>
我知道這是一個不好的解決方案,它可能有問題,我還沒有找到,但只是我的想法...
你如何處理了前兩項移動鼠標,同時將第三個用戶的情況下?你會播放前兩個聲音嗎? – 2011-06-14 15:41:29
實際的代碼會觸發一個thread.sleep的線程,等待100-200毫秒以確保用戶仍然在同一個項目上,在這種情況下,它將檢查ismouseover是否仍然爲真 – tofutim 2011-06-14 15:50:47
不是您的答案問題,但實現你的想法的兩個提示:要在某些事件上播放一些音頻,請將與類一起使用。或者,使用類並處理鼠標事件。 –
2011-06-14 18:09:16