为什么鼠标进入事件火灾鼠标离开甚至箭头仍然在元素的顶部?

时间:2014-08-29 07:51:08

标签: wpf combobox mouseevent

我有一个包含MouseEnter和MouseLeave事件的组合框。当鼠标进入组合框时,它应该并且会触发mouseEnter事件。将其不透明度从40%设置为100%。 MouseLeave事件则相反。这里的问题是当鼠标进入时,它会触发鼠标进入事件,之后鼠标离开事件和鼠标再次进入事件。 Combobox将坚持100%的不透明度。我怎么能做到这一点?

<ComboBox Name="PageNumberComboBox" Panel.ZIndex="10" Width="70" Grid.Row="1" Grid.Column="0" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,10,10,0" IsEditable="True" Opacity="0.4" MouseEnter="PageNumberComboBox_MouseEnter" MouseLeave="PageNumberComboBox_MouseLeave"></ComboBox>

网格:

<Grid Panel.ZIndex="0" Grid.Row="1" Grid.ColumnSpan="2" >

代码:

private void PageNumberComboBox_MouseEnter(object sender, MouseEventArgs e)
    {
        PageNumberComboBox.Opacity = 100.0;
        MessageBox.Show("mouse enter");
    }

    private void PageNumberComboBox_MouseLeave(object sender, MouseEventArgs e)
    {
        PageNumberComboBox.Opacity = 40.0;
        MessageBox.Show("mouse leave");
    }

1 个答案:

答案 0 :(得分:0)

    <ComboBox>
        <ComboBox.Style>
            <Style TargetType="ComboBox">
                <Setter Property="Opacity" Value="0.4"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True">
                        <Setter Property="Opacity" Value="1"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Style>
    </ComboBox>