c#wpf是ComboBox的鼠标悬停事件

时间:2017-10-24 13:06:38

标签: c# wpf user-interface combobox mouseevent

我试图在用户将鼠标悬停在ComboBox的项目上时更新某些值。一个例子是:

enter image description here

此ComboBox的选定项目当前为40.但是在这里我想要在用户将鼠标指针移动到66的值时触发事件。我正在尝试的动作是动态更改字体大小当用户将鼠标悬停在此ComboBox中的数字时,另一个TextBox。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

有点不清楚你想要做什么,但如果你想在鼠标移过ComboBoxItem时执行一些动作,你可以使用ItemContainerStyleDependencyPropertyDescriptor

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>