有没有简单的方法可以做到这一点?
我的问题主要是用户通过控件上的上下文菜单触发了元素创建,然后想要创建一个副本,或者附近的一个新元素等等。
我似乎找不到一种方法来将适当的信息传递给Execute函数。
我知道CommandParameter,但我的直觉是传递整个源代码控制(或者在右键单击菜单的情况下传递它的父控件),这似乎是错误的。
做这件事的惯用方法是什么?
发布于 2014-05-21 03:17:19
您所说的复制级别是多少?
最简单的情况是:
1)在您的ViewModel中使用IEumerable<object>,使用不同的ViewModels(IFunnyControlViewModel、ISadCotrolViewModel)等。
2)在视图中创建ItemsControl并绑定到该集合。您可以使用DataTemplates将不同的视图模型“映射”到不同的视图控件。
3)接收具有底层ViewModel的ViewModel Execute(),只需将其“重新添加”到集合中,从而“引用”相同的对象,如果您想使它们保持同步,或克隆它。
public YourViewModel {
public IEnumerable<YourBaseControlViewModel> Controls {get; set;}
public YourViewModel()
{
Controls = new List<YourBaseControlViewModel();
Controls.Add(new YourFunnyControlViewModel());
}
// Called from View by Command.
public void DuplicateControl(YourBaseControlViewModel Control)
{
// either duplicate it using cloning, or add the same reference.
Controls.Add(Control);
}
}在ItemsControl中,类似于:
<ItemsControl ItemsSource="{Binding Controls}">
<ItemsControl.Resources>
<DataTemplate x:Type="{x:Type blah:YourFunnyControlViewModel}">
custom stuff here
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>https://stackoverflow.com/questions/23766972
复制相似问题