Link to home
Start Free TrialLog in
Avatar of Alyanto
AlyantoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Events in static methods

I am working on a WPF project and need to raise an event back to the control when a value in my Panel's attached dependency MaxX property changes.  When this happens private static void MaxX_PropertyChanged is fired and at that point I want to raise the event.

I have tried this format of event declaration

public static event EventHandler SomeEvent = delegate {};

Open in new window


But I would prefer to have the structure provided by writing it in this manner
        public delegate void MaxWidthChangedHandler(object sender, EventArgs e);
        public event MaxWidthChangedHandler MaxWidthChanged;

Open in new window


so I can consume it like this:
w.WorkCompleted += new Worker.WorkCompletedHandler(this.Handler);

Open in new window

however i get this error

System.NullReferenceException: Object reference not set to an instance of an object.
   at SABER.Performance.Windows.Controls.V3.XPanel.XPanel.MaxX_PropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e) in e:\Saber Development\Saber\Saber Application\SABER.Performance.Windows.Controls.V3\XPanel\XPanel.cs:line 116
This is not exclusively a WPF issue but I admit the use of events in C# is less familiar than that of VB.Net to me.



        #region MaxX or MaxWidth

        public delegate void MaxWidthChangedHandler(object sender, EventArgs e);
        public event MaxWidthChangedHandler MaxWidthChanged;

        /// <summary>
        /// Horizontal position
        /// </summary>
        public static readonly DependencyProperty RelativeXProperty =
            DependencyProperty.RegisterAttached("RelativeX", typeof(double),
                typeof(XPanel),
                new PropertyMetadata(0.5d, InvalidateLayoutCallback));

        static FrameworkPropertyMetadata maxxpropertypmetadata = 
            new FrameworkPropertyMetadata(-0.5,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
        new PropertyChangedCallback(MaxX_PropertyChanged),
        new CoerceValueCallback(MaxX_CoerceValue),
        false, UpdateSourceTrigger.PropertyChanged);

        private static void MaxX_PropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            if ((double)e.NewValue > 0)
            {
                MaxWidthChanged(dobj, new EventArgs());
            }
        }

        private static object MaxX_CoerceValue(DependencyObject dobj, object Value)
        {
            return Value;
        }

        public static readonly DependencyProperty MaxXProperty =  
            DependencyProperty.RegisterAttached("MaxX", typeof(double),
            typeof(XPanel),
            maxxpropertypmetadata, 
            new ValidateValueCallback(MaxX_Validate));

        private static bool MaxX_Validate(object Value)
        {
            //Custom validation block which takes in the value of DP
            //Returns true / false based on success / failure of the validation
            //Observer.Add(string.Format("DataValidation is Fired : Value {0}", Value));
            return true;
        }

        /// <summary>
        /// The relative X to the StickTo Control
        /// </summary>
        public static readonly DependencyProperty DockDistanceXProperty =
            DependencyProperty.RegisterAttached("DockDistanceX", typeof(double),
                typeof(XPanel),
                new PropertyMetadata(0.0d, InvalidateLayoutCallback));
        #endregion

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of louisfr
louisfr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alyanto

ASKER

A very quick response to acknowledge your post Louise.  I am close to the point where I will implement the code you have put forward.  I will respond once I have confirmed it meets the problem I am having.  I understand the null aspect of the problem but I want to try it in a situation where the event is implemented in a static method.  Kind regards Alyanto
Avatar of Alyanto

ASKER

I have tested your code and it does do what I says it well, thank you.