开发者

WPF: Launch code when IsMouseOver ComboBoxItem

I have a ComboBox. Without changing the template, is there a way that I can launch code when there user places their mouse over a ComboBoxItem, but before the selection actually occurs? It seems like I should be able to specify an EventTrigger or a Trigger to do this in the style of 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>

I'm also ok with ousing a MouseEnter, but I would rather not build a separate DataTempla开发者_如何学编程te or ContentTemplate if possible.

Update. The idea behind this snippet is to Play test audio when the user hovers over a new voice, which I would have to do from the code side. Help!


You can use EventSetter:

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

in code behind:

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


I know a dirty solution.. just in case you run out of solutions try this as your last hope..

I tested this by creating a textblock in XAML and setting its text equal to content of comboboxitem once mouse is over it and setting text to "" once mouse has left

I am using AttachedBehaviours to find out on which particular comboboxitem is mouse over once mouse is there and also getting notified once mouse is not over it anymore or mouse is left

Try this.. create a class

 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 = "";
            }
        }
    }

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

I know this is a bad solution and it may have problems which I haven't found yet but just my thoughts...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜