0
votes

We're in WinRT XAML.

I have a custom control B with a dependency property Items, the control is in a template for another control. The dependency property is initialized to an new empty collection using the PropertMetadata type. It is never data-bound - keep that in mind.

When I enter the page, the control is instantiated as part of the template for another custom control A. From a breakpoint in the constructor for B, when I check the value of Items, it contains 0 items.

As part of other activity, control A 'manually' calls B.Items.Add() to insert data. There is no data-binding.

When I navigate backward and then click to navigate to the page again, the constructor is again fired but this time Items still has the values that A added. It retains its value.

So even though the control is unloaded and loaded (I can see the events), the control and dependency property is not reinitialized.

Is this correct?

1

1 Answers

3
votes

The dependency property is declared as a static field in your class B. It is initialized exactly once and hence the new collection in the property metadata is initialized only once and later shared by all "instances" of the property. This is a common mistake made in dependency property declarations.

You should set the dependency property value to a new empty collection in the constructor of class B instead of providing a default value by property metadata.