Question

I am creating a WinRT CustomControl that has a dependency property with PropertyChangedCallback. In that Callback method I try to set values on some of the control's parts that I retrieve from OnApplyMethod using the GetTemplateChild() method.

The problem is that the PropertyChangedCallback is called before OnApplyTemplate so the control parts are still null.

One workaround i found is i am calling this DP's in the load event of my Custom Control. In that case everything works fine for me. But every situations which is not applicable. Suppose if someone want to bind the values through xaml the issue again raises.

Do anyone have any permanent workaround for this issue.

Was it helpful?

Solution

Here's the general pattern I follow when I want to do what you've described:

private void OnFooChanged(...)
{
    if (someNamedPart != null && someOtherNamedPart != null && ...)
    {
        // Do something to the named parts that are impacted by Foo 
        // when Foo changes.
    }
}

private void FooChangedCallback(...)
{
    // Called by WinRT when Foo changes
    OnFooChanged(...)
}

protected override void OnApplyTemplate(...)
{
    // Theoretically, this can get called multiple times - every time the 
    // consumer of this custom control changes the template for this control.
    // If the control has named parts which must react to the properties
    // this control exposes, all that work must be done here EVERY TIME
    // a new template is applied.

    // Get and save named parts as local variables first

    OnFooChanged(...)
}

Hope the pseudo-code helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top