views:

53

answers:

1

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

+1  A: 

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}}"
CommanderZ
re: XAML ordering. I would have thought that too but it doesn't matter in this case; the DowIndexProperty always acquires it's value first. I see no reason why it should do that but it does.
Berryl
default DependencyProperty. Do you mean the DataContext?? If so, the DataGridColumn is not a FrameworkElement, and so does not have one for free. You also can't set a Style for it for the same reason. One of the main reasons I have been approaching this as a subclass with the DPs
Berryl
re sysntax. yeah you're right, that is a more readable binding. Cheers
Berryl
new PropertyMetadata(0, OnDowIndexSet) will set the property default value to 0. See http://msdn.microsoft.com/en-us/library/system.windows.propertymetadata.propertymetadata.aspx
CommanderZ
oh got ya. no, I need the value of what is set in the xaml, so a default value of 0 is a non-starter here
Berryl