Question

The heart of the question in this post is whether you can expect all DPs to be set by the time a Property callback for one of them is set. I ask this because this is not the behavior I am seeing.

A class has two DP's, both of which are set in XAML, like so:

<!-- Days of the Week -->
<local:DayOfTheWeekColumn 
    DowIndex="0" 
    ActivityCollection="{Binding Source={StaticResource spy}, Path=DataContext}"
    ....                    
/>

In the DayOfTheWeekColumn class, the DPs are declared like so, for now:

public static readonly DependencyProperty DowIndexProperty = DependencyProperty.RegisterAttached(
        "DowIndex", typeof(string), typeof(DayOfTheWeekColumn), 
        new PropertyMetadata(OnDowIndexSet), IsIndexValid);

    public static readonly DependencyProperty ActivityCollectionProperty = DependencyProperty.RegisterAttached(
        "ActivityCollection", typeof(IActivityCollectionViewModelBase), typeof(DayOfTheWeekColumn), 
        new PropertyMetadata(OnActivityCollectionSet));

When the OnDowIndexSet callback executes, the ActivityCollectionProperty is still null, but when the OnActivityCollectionSet callback executes, the DowIndexProperty is valued. I need both properties to accomplish this use case. Here is OnActivityCollectionSet:

    private static void OnActivityCollectionSet(DependencyObject target, DependencyPropertyChangedEventArgs e) {
        var context = (IActivityCollectionViewModelBase) e.NewValue;
        var col = (DayOfTheWeekColumn) target;
        var index = Convert.ToInt32(col.DowIndex);
        _setHeader(col, context, index);
    }

Now this works, but it is fragile to me as long as I don't understand the timing of setting both properties by the time the callbacks execute. Why should both properties be available for OnActivityCollectionSet and not OnDowIndexSet?

Cheers,
Berryl

Was it helpful?

Solution

Maybe before the DowIndex is set before ActivityCollection in XAML?

And couldn't you use default DependencyProperty value to prevent this issue?

And small OT tip: The Path binding property is a default one so you could use this shorter notation:

ActivityCollection="{Binding DataContext, Source={StaticResource spy}}"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top