silverlight MouseLeftButtonDown事件总是无法触发

 参考解决办法: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事件中触发,这样我们也就可以用鼠标按钮的事件了。

posted @ 2013-12-16 10:49  wj704  阅读(246)  评论(0编辑  收藏  举报