鼠标事件未在自定义控件C#wpf上触发

时间:2015-09-30 07:36:12

标签: c# wpf events controls

我似乎遇到了一些事件的问题。我希望能够以这种或那种方式注册鼠标上/下/上事件,但无论我做什么,它似乎都没有做到这一点。聘用工作和触发就好了。谁能告诉我什么是错的?

using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace RangeSelector.Controls
{
    public class RangeSelector : Grid
    {
        private static readonly SolidColorBrush DefaultBackground = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));
        private static readonly SolidColorBrush DefaultForeground = new SolidColorBrush(Color.FromArgb(255, 090, 090, 090));

        public RangeSelector()
        {
            Background = DefaultBackground;

            //Create selector
            var polygon = new Polygon
            {
                Points = new PointCollection() { new Point(0,0), new Point(0,10), new Point(10,10), new Point(10,0) },
                Stroke = Brushes.Black,
                Fill = DefaultForeground
            };
            SetValue(SelectionPolygonProperty, polygon);
            //Add events
            polygon.MouseMove += OnMouseMove;
            polygon.MouseWheel += OnMouseWheel;
            polygon.MouseDown += OnMouseDown;
            polygon.MouseUp += OnMouseUp;
            polygon.KeyDown += OnKeyDown;
            polygon.PreviewKeyDown += polygon_PreviewKeyDown;
            //Add child
            Children.Add(polygon);
        }

        #region Overrides

        void polygon_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            throw new System.NotImplementedException();
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if (e.Property == ForegroundProperty)
            {
                GetSelectionPolygon(this).Fill = (Brush)e.NewValue;
            }
        }

        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            base.OnPreviewMouseMove(e);
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
        }
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
        }
        protected override void OnMouseWheel(MouseWheelEventArgs e)
        {
            base.OnMouseWheel(e);
        }
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            UpdateSelectionPolygon();
        }

        public override void EndInit()
        {
            base.EndInit();
            UpdateSelectionPolygon();
        }

        #endregion

        #region Events

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine("Push mah button!");
        }
        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("Mouse has lift off!");
        }
        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("Black mouse down!");
        }
        private void OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            Debug.WriteLine("The wheel on the mouse goes round and round!");
        }
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("Move mice! Get out the way!");
        }

        #endregion

        #region Dependency properties

        public static readonly DependencyProperty MinimumValueProperty =
            DependencyProperty.Register("MinimumValue",
                typeof(double),
                typeof(RangeSelector), 
                new PropertyMetadata(0d));

        public static readonly DependencyProperty MaximumValueProperty =
            DependencyProperty.Register("MaximumValue",
                typeof(double),
                typeof(RangeSelector), 
                new PropertyMetadata(100d));

        public static readonly DependencyProperty LowerValueProperty =
            DependencyProperty.Register("LowerValue",
                typeof(double),
                typeof(RangeSelector), 
                new PropertyMetadata(10d));

        public static readonly DependencyProperty UpperValueProperty =
            DependencyProperty.Register("UpperValue",
                typeof(double),
                typeof(RangeSelector), 
                new PropertyMetadata(90d));

        public static readonly DependencyProperty SelectionPolygonProperty =
            DependencyProperty.Register("SelectionPolygon",
                typeof(Polygon),
                typeof(RangeSelector),
                new PropertyMetadata(null));

        public static readonly DependencyProperty ForegroundProperty =
            DependencyProperty.Register("Foreground",
                typeof (Brush),
                typeof (RangeSelector),
                new PropertyMetadata(DefaultForeground));

        #endregion

        #region XAML accessors

        public static void SetMinimumValue(UIElement element, double value)
        {
            element.SetValue(MinimumValueProperty, value);
        }
        public static double GetMinimumValue(UIElement element)
        {
            return (double)element.GetValue(MinimumValueProperty);
        }

        public static void SetMaximumValue(UIElement element, double value)
        {
            element.SetValue(MaximumValueProperty, value);
        }
        public static double GetMaximumValue(UIElement element)
        {
            return (double)element.GetValue(MaximumValueProperty);
        }

        public static void SetUpperValue(UIElement element, double value)
        {
            element.SetValue(UpperValueProperty, value);
        }
        public static double GetUpperValue(UIElement element)
        {
            return (double)element.GetValue(UpperValueProperty);
        }

        public static void SetLowerValue(UIElement element, double value)
        {
            element.SetValue(LowerValueProperty, value);
        }
        public static double GetLowerValue(UIElement element)
        {
            return (double)element.GetValue(LowerValueProperty);
        }

        public static void SetSelectionPolygon(UIElement element, Polygon value)
        {
            element.SetValue(SelectionPolygonProperty, value);
        }
        public static Polygon GetSelectionPolygon(UIElement element)
        {
            return (Polygon)element.GetValue(SelectionPolygonProperty);
        }

        public static void SetForeground(UIElement element, Brush value)
        {
            var polygon = GetSelectionPolygon(element);
            polygon.Fill = value;
            element.SetValue(ForegroundProperty, value);
        }
        public static Brush GetForeground(UIElement element)
        {
            return (Brush) element.GetValue(ForegroundProperty);
        }

        #endregion

        private void UpdateSelectionPolygon()
        {
            var polygon = (Polygon)GetValue(SelectionPolygonProperty);
            if (polygon == null) return;

            var lowerValue = ActualWidth / 3;
            var upperValue = ActualWidth - (ActualWidth / 3);

            polygon.Points[0] = new Point(lowerValue, 0);
            polygon.Points[1] = new Point(lowerValue, ActualHeight);
            polygon.Points[2] = new Point(upperValue, ActualHeight);
            polygon.Points[3] = new Point(upperValue, 0);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我使用了你的控件:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1.RangeSelector.Controls"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:RangeSelector/>
    </Grid>
</Window>

所有事件都是正确解雇而没有问题。在使用您的控件时,您可能正在使用XAML代码执行其他操作。