C# 如何从视图模型关闭用户控件
我创建了我的C# 如何从视图模型关闭用户控件,c#,wpf,mvvm,caliburn.micro,dynamic-usercontrols,C#,Wpf,Mvvm,Caliburn.micro,Dynamic Usercontrols,我创建了我的UserControl,如下所示: MyUserCtrl myctrl = new MyUserCtrl() { DataContext = new MyViewModel()}; ControlCollection.Add(myctrl); 我使用这个itemscontrolitemssource=“{Binding ControlCollection}”将它输出到视图 它干净漂亮,但问题是我不知道如何关闭我打开的UserControls 如果我把它移到收藏中呢。因此视图模型也将
UserControl
,如下所示:
MyUserCtrl myctrl = new MyUserCtrl() { DataContext = new MyViewModel()};
ControlCollection.Add(myctrl);
我使用这个itemscontrolitemssource=“{Binding ControlCollection}”
将它输出到视图
它干净漂亮,但问题是我不知道如何关闭我打开的UserControls
如果我把它移到收藏中呢。因此视图模型也将关闭?不要将UI元素的集合分配给ItemsControl的ItemsSource。相反,将UI元素放在ItemsControl的
ItemTemplate
中,并将视图模型实例的集合传递给ItemsSource
<ItemsControl ItemsSource="{Binding MyItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MyUserCtrl />
</DataTemplate>
</ItemsCControl.ItemTemplate>
</ItemsCControl>
要“关闭”控件,请从集合中删除相应的项:
MyItems.Remove(item);
“干净又漂亮”-不太好。谢谢你的提示。
MyItems.Remove(item);