简体   繁体   English

MVVM Light-如何向网格添加(动态)用户控件?

[英]MVVM Light - how to add (dynamic) usercontrol to the grid?

I'm trying to figure out how to add UserControl to the Grid depending on the user's choice. 我试图弄清楚如何根据用户的选择将UserControl添加到网格中。

Short description: 简短的介绍:

I have ComboBox: 我有ComboBox:

  • ItemSource binded to ObservableCollection<Products> , ItemSource绑定到ObservableCollection<Products>
  • SelectedItem binded to Product . SelectedItem绑定到Product
  • Inside the MainPage.xaml I have <Grid> MainPage.xaml我具有<Grid>

I have products like: Doors , Lamps , Chairs etc... All of them inherit from Product class. 我有类似的产品: DoorsLampsChairs等。它们都继承自Product类。

Looking for a hints/solutions: 寻找提示/解决方案:

I have many UserControls with ViewModels for Products : One for Doors , one for Lamps , one for Chairs . 我有许多用于Products ViewModel的UserControl:一个用于Doors ,一个用于Lamps ,一个用于Chairs

Depending on user choice I would like to add selected product UserControl to the <Grid> 根据用户选择,我想将选定的产品UserControl添加到<Grid>

Example: 例:

  • User selects "Chairs" from the Combobox, another ChairsUserControl.xaml will be added to the <Grid> 用户从组合框中选择“主席”,另一个ChairsUserControl.xaml将添加到<Grid>
  • User selects "Doors" from the Combobox, another DoorsUserControl.xaml will be added to the <Grid> 用户从组合框中选择“门”,另一个DoorsUserControl.xaml将添加到<Grid>

and so on... Grid may have many same UserControls (they can repeat). 依此类推...网格可能具有许多相同的UserControl(它们可以重复)。

many thanks for any hints! 非常感谢任何提示!

答案是:DataTemplateSelector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM