VM instance bound to a UI element is not destroyed even after the bind target is nulled.
Posted: 13 Jun 2019, 10:07
So we have a code like this
xaml
C++
We expect that when the ButtonCmd is fired, m_item is reset, then the ItemVM instance we created will be destroyed. However, it seems that the FrameworkElement still hold the reference preventing the destruction.
(as for the appearance, the ItemVM is correctly disappeared from the UI after the reset)
If for example, we create another instance of ItemVM and assign it to m_item, then the previous instance is correctly destroyed.
xaml
Code: Select all
<Grid>
<ContentControl Content="{Binding CurrentItem}" />
</Grid>
Code: Select all
class ItemVM : public BaseComponent, public INotifyPropertyChanged
{
...
}
class BindingCollectionVM : public BaseComponent, public INotifyPropertyChanged
{
public:
Ptr<ItemVM> m_item = MakePtr<ItemVM>(); // create ItemVM here
void ButtonCmd() {
m_item.Reset(); // Clear the item when a button is clicked
_propertyChanged(this, NSS(CurrentItem));
}
NS_IMPLEMENT_INLINE_REFLECTION(BindingCollectionVM, BaseComponent)
{
...
NsProp("CurrentItem", &BindingCollectionVM::m_item );
}
(as for the appearance, the ItemVM is correctly disappeared from the UI after the reset)
If for example, we create another instance of ItemVM and assign it to m_item, then the previous instance is correctly destroyed.
Code: Select all
void ButtonCmd() {
m_item = MakePtr<ItemVM>();
_propertyChanged(this, NSS(CurrentItem));
}