Mouse event not firing on custom control C# wpf -
i seem have problem events. want able to, 1 way or another, register mouse over/down/up-events, whatever do, doesn't seem trick. resiring works , triggers fine. can tell me what's wrong?
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 mouse goes round , round!"); } private void onmousemove(object sender, mouseeventargs e) { debug.writeline("move mice! out 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); } } }
i have used control :
<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>
and events fired correctly without issues. may doing thing else in xaml code while using control.
Comments
Post a Comment