首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过命令创建新的UIElements

通过命令创建新的UIElements
EN

Stack Overflow用户
提问于 2014-05-21 02:08:45
回答 1查看 146关注 0票数 0

有没有简单的方法可以做到这一点?

我的问题主要是用户通过控件上的上下文菜单触发了元素创建,然后想要创建一个副本,或者附近的一个新元素等等。

我似乎找不到一种方法来将适当的信息传递给Execute函数。

我知道CommandParameter,但我的直觉是传递整个源代码控制(或者在右键单击菜单的情况下传递它的父控件),这似乎是错误的。

做这件事的惯用方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2014-05-21 03:17:19

您所说的复制级别是多少?

最简单的情况是:

1)在您的ViewModel中使用IEumerable<object>,使用不同的ViewModels(IFunnyControlViewModel、ISadCotrolViewModel)等。

2)在视图中创建ItemsControl并绑定到该集合。您可以使用DataTemplates将不同的视图模型“映射”到不同的视图控件。

3)接收具有底层ViewModel的ViewModel Execute(),只需将其“重新添加”到集合中,从而“引用”相同的对象,如果您想使它们保持同步,或克隆它。

代码语言:javascript
复制
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中,类似于:

代码语言:javascript
复制
<ItemsControl ItemsSource="{Binding Controls}">
   <ItemsControl.Resources>
     <DataTemplate x:Type="{x:Type blah:YourFunnyControlViewModel}">
        custom stuff here
     </DataTemplate>
   </ItemsControl.Resources>
</ItemsControl>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23766972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档