user1011524
user1011524

Reputation: 41

WPF Command - Memory Leak

I'm using WPF 4 with the MVVM pattern. I have a TabControl - it looks like:

 <TabControl x:Name="Items" ItemsSource="{Binding Screens}" 
                            SelectedItem="{Binding ActiveScreen}">
   <TabControl.ItemTemplate>
     <DataTemplate>
       <ContentControl>
            <Button Command="{Binding 
                              DataContext.ScreenCloseCommand, 
                              ElementName=MainWindow}" 
                    CommandParameter="{Binding}">X</Button>
       </ContentControl>
     </DataTemplate>
   </TabControl.ItemTemplate>

The problem is, that CommandParameter="{Binding}" holds a strong reference to the currently displayed View/ViewModel. How can i remove that command? In other words: i need a way to remove the Button Command from ItemTemplate by only having a reference of the TabControl.ContentTemplate (ViewModel and View). Something like go through items, find the item, remove command ...

Thanks Michael

Upvotes: 4

Views: 649

Answers (1)

svick
svick

Reputation: 244797

I think that you're looking at the wrong problem. It's not the Binding that keeps the ViewModel alive. It's the whole TabControl and the collection it binds to.

If you described what actual problem are you trying to solve, and not the solution you're unsuccessfully trying to use, we might help you better.

Upvotes: 1

Related Questions