打印

silverlight1.1 的自定义控件, 加了一个类似鼠标点击的自定义事件

例子是微软的, 在代码里加了一个MouseRelease的事件, 模仿鼠标的click事件,
代码比较乱, 随便看看
复制内容到剪贴板
代码:
<Canvas xmlns="http://schemas.microsoft.com/client/2007"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="130" Height="30"
        Background="Silver"
       >
  <TextBlock x:Name="tb"></TextBlock>
</Canvas>
复制内容到剪贴板
代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace CustomerControl
{
    public class MyLabel : Control
    {
        public delegate void mouseRelease(object sender, MouseEventArgs e);
        public event mouseRelease MouseRelease;
        bool mouseInthis=false, mouseDown=false;
        FrameworkElement implementationRoot;
        TextBlock tb;
        public MyLabel()
        {
            System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("CustomerControl.MyLabel.xaml");
            implementationRoot = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
            tb = implementationRoot.FindName("tb") as TextBlock;
            this.Loaded += new EventHandler(MyLabel_Loaded);
            implementationRoot.MouseEnter += new MouseEventHandler(onMouseEnter);
            implementationRoot.MouseLeave += new EventHandler(onMouseLeave);
            implementationRoot.MouseLeftButtonDown += new MouseEventHandler(onMouseDown);
            implementationRoot.MouseLeftButtonUp += new MouseEventHandler(onMouseUp);
            base.Width = implementationRoot.Width;
            base.Height = implementationRoot.Height;
        }
        void MyLabel_Loaded(object sender, EventArgs e)
        {
            UpdateLayout();
        }
        public virtual new double Height
        {
            get
            {
                return implementationRoot.Height;
            }
            set
            {
                implementationRoot.Height = value;
                UpdateLayout();
            }
        }
        public virtual new double Width
        {
            get
            {
                return implementationRoot.Width;
            }
            set
            {
                implementationRoot.Width = value;
                UpdateLayout();
            }
        }
        public String Text
        {
            get
            {
                return tb.Text;
            }
            set
            {
                tb.Text = value;
                UpdateLayout();
            }
        }
        public SolidColorBrush LabelColor
        {
            get
            {
                return (SolidColorBrush)tb.Foreground;
            }
            set
            {
                tb.Foreground = (SolidColorBrush)value;
            }
        }
        protected void UpdateLayout()
        {
            tb.SetValue(Canvas.LeftProperty, (Width - tb.ActualWidth) / 2);
            tb.SetValue(Canvas.TopProperty, (Height - tb.ActualHeight) / 2);
            //tb.SetValue(Canvas.LeftProperty, 0);
            //tb.SetValue(Canvas.TopProperty, 0);
        }
        void onMouseEnter(object sender, MouseEventArgs e)
        {
            mouseInthis = true;
        }
        void onMouseLeave(object sender, EventArgs e)
        {
            mouseInthis = false;
            mouseDown = false;
        }
        void onMouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
        }
        void onMouseUp(object sender, MouseEventArgs e)
        {
            if (mouseDown && mouseInthis)
            {
                mouseDown = false;
                MouseRelease(sender, e);            
            }
        }
    }
}
修改了一下, 现在基本上能模拟了
在按钮上按住鼠标移开再回来也能正确响应了
复制内容到剪贴板
代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace CustomerControl
{
    public class MyLabel : Control
    {
        public delegate void mouseRelease(object sender, MouseEventArgs e);
        public event mouseRelease MouseRelease;
        bool mouseInthis=false, mouseDown=false;
        FrameworkElement implementationRoot;
        TextBlock tb;
        Canvas can;
        public MyLabel()
        {
            System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("CustomerControl.MyLabel.xaml");
            implementationRoot = this.InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
            tb = implementationRoot.FindName("tb") as TextBlock;
            can = implementationRoot as Canvas;
            this.Loaded += new EventHandler(MyLabel_Loaded);
            implementationRoot.MouseEnter += new MouseEventHandler(onMouseEnter);
            implementationRoot.MouseLeave += new EventHandler(onMouseLeave);
            implementationRoot.MouseLeftButtonDown += new MouseEventHandler(onMouseDown);
            implementationRoot.MouseLeftButtonUp += new MouseEventHandler(onMouseUp);
            base.Width = implementationRoot.Width;
            base.Height = implementationRoot.Height;
        }
        void MyLabel_Loaded(object sender, EventArgs e)
        {
            UpdateLayout();
        }
        public double Ht
        {
            get
            {
                return implementationRoot.Height;
            }
            set
            {
                implementationRoot.Height = value;
                UpdateLayout();
            }
        }
        public virtual new double Height
        {
            get
            {
                return implementationRoot.Height;
            }
            set
            {
                implementationRoot.Height = value;
                UpdateLayout();
            }
        }
        public virtual new double Width
        {
            get
            {
                return implementationRoot.Width;
            }
            set
            {
                implementationRoot.Width = value;
                UpdateLayout();
            }
        }
        //Width属性用new来声明也不行, 在前台直接赋值好像是对base类的属性赋值了
        public double Wd
        {
            get
            {
                return implementationRoot.Width;
            }
            set
            {
                implementationRoot.Width = value;
                UpdateLayout();
            }
        }
        public String Text
        {
            get
            {
                return tb.Text;
            }
            set
            {
                tb.Text = value;
                UpdateLayout();
            }
        }
        public SolidColorBrush LabelColor
        {
            get
            {
                return (SolidColorBrush)tb.Foreground;
            }
            set
            {
                tb.Foreground = (SolidColorBrush)value;
            }
        }
        protected void UpdateLayout()
        {
            tb.SetValue(Canvas.LeftProperty, (Width - tb.ActualWidth) / 2);
            tb.SetValue(Canvas.TopProperty, (Height - tb.ActualHeight) / 2);
            //tb.SetValue(Canvas.LeftProperty, 0);
            //tb.SetValue(Canvas.TopProperty, 0);
        }
        void onMouseEnter(object sender, MouseEventArgs e)
        {
            mouseInthis = true;
        }
        void onMouseLeave(object sender, EventArgs e)
        {
            mouseInthis = false;            
        }
        void onMouseDown(object sender, MouseEventArgs e)
        {
            implementationRoot.CaptureMouse();
            mouseDown = true;
        }
        void onMouseUp(object sender, MouseEventArgs e)
        {
            implementationRoot.ReleaseMouseCapture();
        
            if (mouseDown && mouseInthis)
            {
                mouseDown = false;
                MouseRelease(sender, e);            
            }
            else if(!mouseInthis)
                mouseDown = false;
        }
    }
}