有一种简单的方法吗?
我的问题主要是用户通过控件上的上下文菜单触发元素创建,然后想要创建副本或附近的新元素等。
我似乎无法找到将相应信息传递给Execute
函数的方法。
我知道CommandParameter
但我的直觉是传递整个源控件(或右键菜单中的父控件),这似乎是错误的。
这样做的惯用方法是什么?
答案 0 :(得分:2)
你在谈论什么级别的重复?
最简单的情况:
1)ViewModel中的IEumerable<object>
,具有不同的ViewModel(IFunnyControlViewModel,ISadCotrolViewModel)等。
2)在View中创建ItemsControl并绑定该集合。您可以使用DataTemplates来&#34;映射&#34;不同的视图模型到不同的视图控件。
3)使用基础ViewModel接收ViewModel Execute(),只需&#34;重新添加&#34;它集合,因此&#34;引用&#34;同一个对象,如果你想让它们保持同步,或者克隆它。
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>