Question

I'm using usercontrol in windows phone development. The custom control has a property: ButtonsCountProperty

public static readonly DependencyProperty ButtonsCountProperty = DependencyProperty.Register(
            "ButtonsCount", 
            typeof(int), typeof(WCCSegmentControl), 
            new PropertyMetadata(0, new PropertyChangedCallback(ButtonsCountChanged)));

In the callback method ButtonsCountChanged, I write:

button.Click += new RoutedEventHandler(segmentcontrolButtonClick);

The error occurred.

An object reference is required for the non-static field, method, or property

Anybody know how to write the button click event in the PropertyChangedCallback static method? Thank you.

Was it helpful?

Solution 2

Finally I know the solution. I create an instance of the current class:

public static WCCSegmentControl _wccSegmentControl;

And I add this to the static method:

button.Click += new RoutedEventHandler(_wccSegmentControl.segmentcontrolButtonClick);

It works perfectly.

OTHER TIPS

because ButtonsCountChanged is a static method, you need to cast to the owner object

private static void ButtonsCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
    WCCSegmentControl control = (WCCSegmentControl)d;
    control.button.Click += new RoutedEventHandler(segmentcontrolButtonClick);
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top