WPF:在IsMouseOver ComboBoxItem时启动代码

时间:2011-06-14 15:32:43

标签: c# wpf

我有一个ComboBox。在不更改模板的情况下,有什么方法可以在用户将鼠标放在ComboBoxItem上时,但在选择实际发生之前启动代码?似乎我应该能够以ComboBoxItem的样式指定EventTrigger或Trigger来执行此操作。

<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>

我也可以使用MouseEnter,但如果可能的话,我宁愿不构建单独的DataTemplate或ContentTemplate。

更新。此片段背后的想法是当用户将鼠标悬停在新语音上时播放测试音频,我必须从代​​码端进行操作。救命啊!

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

我知道一个肮脏的解决方案..以防万一你的解决方案用尽了,试试这是你最后的希望......

我通过在textblock中创建XAML并在text content comboboxitem mouse后设置over来对此进行测试1}}并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 = ""; } } } ,并且一旦鼠标不再通过它还会收到通知或留下鼠标

试试这个..创建一个类

 <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>

在XAML中

{{1}}

我知道这是一个糟糕的解决方案,它可能有我尚未找到的问题但只是我的想法......