1

I am trying to update some values while the user hovers over the Items of a ComboBox. An example would be:

enter image description here

The selected item of this ComboBox is currently 40. But here I would like to have an event to be fired when user moves the mouse pointer over the value of 66. The action I am trying is to dynamically change the font size in another TextBox when user hovers through the numbers inside this ComboBox. Thanks for any help.

4
  • 1
    Sure you can. Implement a hover method for the comboBox´s elements. Just bind the event in XAML and implement the method in your code. By the way, that´s not how SO works. Put some effort in research and try it on your own before asking. Commented Oct 24, 2017 at 13:19
  • What is the name of this hover event? Is it myComboBox.Items.CurrentChanging or another thing? And also how can I catch the item that is being under hover currently? Commented Oct 24, 2017 at 13:26
  • @alikerimerkan CurrentChanging sounds pretty unlikely based on the name. Mouse events have the word "mouse" in the name. You can look this up on your own. You can google the MSDN documentation, or just look through the properties with intellisense. The events have little lightning bolt icons next to them. Commented Oct 24, 2017 at 13:29
  • google.de/… Just put some effort in it. That´s what programming is about. Commented Oct 24, 2017 at 13:32

1 Answer 1

2

It's a bit unclear what you are trying to do but if you want to perform some action when the mouse moves over a ComboBoxItem, you could use an ItemContainerStyle and a DependencyPropertyDescriptor:

DependencyPropertyDescriptor dpd;
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
    ComboBoxItem cmb = sender as ComboBoxItem;
    dpd = DependencyPropertyDescriptor
        .FromProperty(IsMouseOverProperty, typeof(ComboBoxItem));
    if (dpd != null)
        dpd.AddValueChanged(cmb, OnIsMouseOver);

}

private void ComboBox_Unloaded(object sender, RoutedEventArgs e)
{
    if (dpd != null)
        dpd.RemoveValueChanged(cmb, OnIsMouseOver);

}

private void OnIsMouseOver(object sender, EventArgs e)
{
    ComboBoxItem cmb = sender as ComboBoxItem;
    if (cmb.IsMouseOver)
    {
        //do something...
    }
}

<ComboBox x:Name="cmb">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <EventSetter Event="Loaded" Handler="ComboBox_Loaded" />
            <EventSetter Event="Unloaded" Handler="ComboBox_Unloaded" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
Sign up to request clarification or add additional context in comments.

1 Comment

That helped to respond the events when user hovers over the values of combobox. However I implemented the click events handler like: cmb.PreviewMouseLeftButtonDown += OnMouseLeftBtnDown;. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.