Question

I'm developing a windows phone 8 app. I'm using a DependencyProperty in my UserControl. I found that the PropertyChangedCallback in the constructor of PropertyMetadata is only triggered when the property value is changed. I have problem with this.

public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
        "IsSelected", typeof(bool), 
        typeof(WCCSelectableButtonImageText), 
        new PropertyMetadata(
            false, 
            new PropertyChangedCallback(IsSelectedPropertyChanged))
        );

In my code, I have a property IsSelected, I want to do something when set the value to it, no matter the value is default or changed. But according this, we know that we can't do anything in the set definitions of the wrapper. I also find this, I'm sure CoerceValueCallback will be called no matter the property is default or changed. But in windows phone SDK, I find I can't use CoerceValueCallback. Anybody know how to call a function when set the value to a DependencyProperty? Thank you very much

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top