How to check if mouse is over a button in WPF? [duplicate]

余生颓废 提交于 2019-12-12 05:32:07

问题


Possible Duplicate:
Time Delay on Trigger

I am using a few buttons in WPF for displaying images from various folders. The Click mode for all the buttons is Hover. For some button, after hovering, it is routed to a function OnButtonClick. What I want to do is, ONLY after the mouse is over the button for X seconds, the processing inside the function OnButtonClick should be done.

EDIT: XAML Code:

    <Button Name="Dresses" Content="Dresses" Click="OnButtonClickDresses" Grid.Row="2" Height="34" Width="85" VerticalAlignment="Center" Grid.Column="5"  FontFamily="Tahoma" FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" Cursor="Hand" ClickMode="Hover">
        <Button.Background>
            <LinearGradientBrush>
                <GradientStop Color="Yellow" Offset="0" />
                <GradientStop Color="Green" Offset="1" />
            </LinearGradientBrush>
        </Button.Background>
    </Button>

C# Code:

    private void OnButtonClickDresses(object sender, RoutedEventArgs e)
    {
            //Code for delay

            //Code which should run only if mouse on button after 5 seconds

    }

回答1:


Use a trigger on the MouseOver event. Once the mouse is over your control, start a timer. If it elapses while the mouse is still over the control, simulate a click.

See a small example here



来源:https://stackoverflow.com/questions/12908968/how-to-check-if-mouse-is-over-a-button-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!