Memory leak when binding commands
Posted: 04 Aug 2021, 00:47
Hi guys! I ran into a problem and got stuck. I'm new to wpf and mvvm, maybe I'm missing something.
Binding to a command which is up the hierarchy causes memory leaks.
Yeah, I saw this topic. But in the context of commands, I don't know how to get around it.
I have a App UserControl, which consists only of ContentControl in the xaml. Depending on the App ViewModel, a menu is "inserted" into this ContentControl. Menus are also UserControls with their View Models (which are the fields of the App ViewModel). View Models of menus contains ViewModels of panels (also UserControls) and so on.
For clarity:
Everything is fine at this stage, but here's what's next.
I binding the UserControl command to the element of this UserControl. And since the command is a field of the View Model of this control, a memory leak occurs due to a known issue.
Perhaps I need to somehow pass the datacontext from top to bottom in the hierarchy to avoid leaks? Or, in principle, I need to do architecturally differently?
Thanks for any help
Binding to a command which is up the hierarchy causes memory leaks.
Yeah, I saw this topic. But in the context of commands, I don't know how to get around it.
I have a App UserControl, which consists only of ContentControl in the xaml. Depending on the App ViewModel, a menu is "inserted" into this ContentControl. Menus are also UserControls with their View Models (which are the fields of the App ViewModel). View Models of menus contains ViewModels of panels (also UserControls) and so on.
For clarity:
Code: Select all
class ApplicationVM : public NotifyPropertyChangedBase
{
public:
FirstMenuVM _first_menu_vm;
SecondMenuVM _second_menu_vm;
...
};
Code: Select all
class FirstMenuVM : public NotifyPropertyChangedBase
{
public:
LeftPanelVM _left_panel_vm;
TopPanelVM _top_panel_vm;
...
};
I binding the UserControl command to the element of this UserControl. And since the command is a field of the View Model of this control, a memory leak occurs due to a known issue.
Code: Select all
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="clr-namespace:D" x:Class="D.LibraryInlLeftPanel">
<UserControl.Resources>
<ItemsPanelTemplate x:Key="LibraryPanelTemplate">
<WrapPanel/>
</ItemsPanelTemplate>
</UserControl.Resources>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ItemsControl DataContext="{Binding LibraryInlLeftPanelViewModel}" ItemsSource="{Binding ItemsObservableCollection }" ItemsPanel="{StaticResource LibraryPanelTemplate}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<d:ItemPreview ItemName="{Binding ItemName}" ItemSource="{Binding ItemSource}" Command="{Binding DataContext.Command, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type d:LibraryInlLeftPanel}}}" CommandParameter="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</UserControl>
Thanks for any help