Question

I have a dependency property setup within a custom control as follows:

    public IChartData Data
    {
        get
        {
           return (IChartData)GetValue(DataProperty);
        }
        set
        {
            SetValue(DataProperty, value);
        }
    }

    public static readonly DependencyProperty DataProperty = DependencyProperty.Register
                                                             (
                                                                "Data",
                                                                typeof(IChartData),
                                                                typeof(ChartViewUserControl),
                                                                new FrameworkPropertyMetadata() { PropertyChangedCallback = UpdateCharting }
                                                             );

and its PropertyChangedCallback as such:

    private static void UpdateCharting(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Stuff Happens Here
    }

As you can see, the dependency property type is IChartData. The reason is that I could be passing one of two types (SimpleChartData or ComplexChartData.) These two types both extend a collection as such:

    public class SimpleChartData : ObservableCollection<ChartDataItem>, IChartData

and

    public class ComplexChartData : ObservableCollection<SimpleChartData>, IChartData

My issue is that if I create an instance of one of these types and add to the collection, then the PropertyChangedCallback function fires as expected. However, if I need to clear the collection, then I just instantiate it as new but the callback function does not fire. I could create a hack everywhere that the control is used but that's obviously not ideal. How can I get the callback to function to fire when the custom type is instantiated. Other suggestions to solve my problem is of course welcome.

Was it helpful?

Solution 2

Sigh... I am a complete idiot. Michael Gunter had commented and pointed out my mistake of posting the wrong callback method. My issue was a combination of carelessness and poor naming. Not only had I posted the wrong method in my code, I had also been checking the wrong method for firing when the Data property was changed. It was working correctly all along and my initial problem was elsewhere. I'm sorry to bother everyone with such a stupid mistake on my part. I do appreciate, however, everyone's input and the extra education that NETscape provided. Thanks to all.

OTHER TIPS

This approach is pretty dirty but will work. Changing value to null and back to old will force you propertychanged callback.

collection.Clear();
foreach(var item in list)
{
   // refresh data
   var temp = item.Data;
   item.Data = null;
   item.Data = temp;
   collection.Add(item);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top