Assuming IsSelectedPropertyChanged conforms to
protected static void IsSelectedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
the issue is that because this method is static it is not able to access the variables in the class. Since the actual properties(variables) that correlate with the DependencyProperty are not static themselves, and so the static property changed handler does not know about the properties/variables that are specific to an instance.
i.e. IsSelected is an instance property, but the DependencyProperty IsSelectedProperty is static, and its value changed callback is static also.
public bool IsSelected
{
get{(bool)GetValue(IsSelectedProperty)}
set{SetValue(IsSelectedProperty, value)}
}
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected", typeof(bool), type(thisCustomControlClassName), new PropertyMetadata(false, thisCustomControlClassName.IsSelectedPropertyChanged);
protected static void IsSelectedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
So you can NOT do the following:
protected static void IsSelectedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
IsSelected = true; //(IsSelected (instance properties) not available in the body of a static method)
}
the Solution is to cast the DependencyObject parameter as an instance of the class, then you can access all of its instance properties and methods.
The DependencyPropertyChangedEventArgs will contain the value of IsSelectedProperty before the state change as
e.OldValue
and the new value for the IsSelectedProperty in
e.NewValue
where e.NewValue and e.OldValue are of type object and will need to be cast as the type your expecting (bool in this example).
i.e.
protected static void IsSelectedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var control = (thisCustomControlClassName)o;
control.IsSelected = (bool)e.NewValue;
}