VM instance bound to a UI element is not destroyed even after the bind target is nulled.
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));
}
-
-
sfernandez
Site Admin
- Posts: 2702
- Joined:
Re: VM instance bound to a UI element is not destroyed even after the bind target is nulled.
Hello,
I've seen that DataContext value is not reset when ContentPresenter.Content gets updated and is keeping alive the old value until the next layout pass occurs. This is something we need to fix, because in that same situation WPF correctly resets the DataContext.
I also verified it doesn't matter if you reset the item or create a new one, the old item always stays alive, the problem lies in ContentPresenter.DataContext property.
Could you please open this issue in the bugtracker, we will fix it as soon as possible.
I've seen that DataContext value is not reset when ContentPresenter.Content gets updated and is keeping alive the old value until the next layout pass occurs. This is something we need to fix, because in that same situation WPF correctly resets the DataContext.
I also verified it doesn't matter if you reset the item or create a new one, the old item always stays alive, the problem lies in ContentPresenter.DataContext property.
Could you please open this issue in the bugtracker, we will fix it as soon as possible.
-
-
sfernandez
Site Admin
- Posts: 2702
- Joined:
Re: VM instance bound to a UI element is not destroyed even after the bind target is nulled.
Thanks for the report.
Who is online
Users browsing this forum: Semrush [Bot] and 3 guests