silverlight MouseLeftButtonDown事件总是无法触发

本文详细解析了在Silverlight中如何正确处理按钮的MouseLeftButtonDown和MouseLeftButtonUp事件,并介绍了通过设置ClickMode属性来调整事件行为的方法。

 参考解决办法:http://www.cnblogs.com/tianguook/archive/2011/05/13/2045299.html

 在构造函数中首先添加一个事件:

 public BtnLists()
        {
            InitializeComponent();
            btnCredit.AddHandler(BtnLists.MouseLeftButtonDownEvent, new MouseButtonEventHandler(this.btnCredit_MouseLeftButtonDown), true);
        }

 然后此事件就可以触发了:

 #region  账户管理按钮的单击事件 
        private void btnCredit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OnCreditClick(this,e);
        }
        public delegate void CreditClickHandler(object sender, RoutedEventArgs e);

        public event CreditClickHandler CreditClick;

        private void OnCreditClick(object sender, RoutedEventArgs e)//事件的注册
        {
            if (CreditClick != null)
            {
                CreditClick(this, e);
            }
        }
       #endregion

  翻译一下,从这个网站看到Button 的这两个事件无法触发的解决办法  http://www.silverlightshow.net/items/Tip-How-to-handle-the-MouseLeftButtonDown-and-MouseLeftButtonUp-events-of-the-Button-control.aspx

 原文:

 
Have you ever noticed that the MouseLeftButtonDown and MouseLeftButtonUp events are not fired when a Silverlight button is clicked? The reason for this is that the button handles these two events itself by overriding the OnMouseLeftButtonDown  and the OnMouseLeftButtonUp  handlers. In the OnMouseLeftButtonDown  override, the Click event is raised and the MouseLeftButtonDown event is marked as handled so it couldn't bubble in the visual tree. The OnMouseLeftButtonUp  override also marks the MouseLeftButtonUp  as handled.

This thing can be changed using the ClickMode property of the Button control. It has the following values - Hover, Press, Release. The default one is Pressed and we have already explained it. When we have ClickMode set to Release, the Click event will be raised in the OnMouseLeftButtonUp override and the MouseLeftButtonDown and MouseLeftButtonUp events will be handled inside the button again. If we set the ClickMode to Hover, the Click event will be raised with the MouseEnter event and we will also be able to use the mouse button events.

That's it!

 译:

 你曾经是否注意到当一个Siverlight 按钮被单击时,MouseLeftButtonDown和MouseLeftButtonUp这两个事件并没有被触发?这个现象的原因是这个按钮通过重写OnMouseLeftButtonDown和OnMouseLeftButtonUp 自发地处理了这两个事件。在OnMouseLeftButtonDown重写中,单击事件被触发、MouseLeftButtonDown事件被标志为已处理,所以它没有被触发(翻译有出入,这个不知道怎么翻译。。so it couldn't bubble in the visual tree)。在OnMouseLeftButtonUp重写方法中同样也将MouseLeftButtonUp 设置为已处理。

 可以通过改变Button 按钮的ClickMode属性来改变这个事实。它有以下属性值:Hover,Press,Release. 默认的是Pressed,这个我们已经解释了。当我们将ClickMode的属性设置为Release时,Click事件在OnMouseLeftButtonUp重写方法中会被触发,MouseLeftButtonDown和MouseLeftButtonUp事件在这个按钮里面又会被处理。如果我们将ClickMode的属性设置为Hover,这个单击事件会在MouseEnter事件中触发,这样我们也就可以用鼠标按钮的事件了。

转载于:https://www.cnblogs.com/wj204/p/3476289.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值