WPF 命令 - 内存泄漏
我正在使用 WPF 4 和 MVVM 模式。我有一个 TabControl - 它看起来像:
<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>
问题是,CommandParameter="{Binding}" 拥有对当前显示的 View/ViewModel 的强引用。我怎样才能删除该命令?换句话说:我需要一种方法,通过仅引用 TabControl.ContentTemplate (ViewModel 和 View)来从 ItemTemplate 中删除按钮命令。比如浏览项目、找到项目、删除命令……
谢谢 迈克尔
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找错误的问题。使 ViewModel 保持活动状态的不是
Binding
。它是整个TabControl
及其绑定到的集合。如果您描述了您想要解决的实际问题,而不是您尝试使用但未成功的解决方案,我们可能会更好地帮助您。
I think that you're looking at the wrong problem. It's not the
Binding
that keeps the ViewModel alive. It's the wholeTabControl
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.