根据MVVM模式动态创建控件 [英] Dynamically Creating Controls Following MVVM pattern

查看:305
本文介绍了根据MVVM模式动态创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Silverlight应用程序中动态生成一些控件.
更清楚地说,这是我的课程的简化定义:

I'd like to dynamically generate some controls in my silverlight application.
To be more clear, here's a simplified definition of my class:

public class TestClass
{
    [Display(Name="First Name")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    public List<CustomProperty> CustomProperties { get; set; }
}

每个"CustomProperty"最终将是一个TextBox,CheckBox或ComboBox:

Each "CustomProperty" will finally be a TextBox, CheckBox or ComboBox:

public class CustomProperty
{
    public CustomDataType DataType { get; set; } //enum:integer, string, datetime, etc
    public object Value { get; set; }
    public string DisplayName { get; set; }
    public string Mappings { get; set; } // Simulating enums' behavior.
}

  • 使用MVVM模式实现此目的的最佳方法是什么?如果我在ViewModel中解析CustomProperty,并找出应创建的控件,那么如何基于MVVM模式在视图中创建新控件.

    • What is the best way to implement this using MVVM pattern? If I parse CustomProperties in ViewModel, and find out which controls should be created, How can I create new controls in my view based on MVVM pattern.

      是否有任何Silverlight控件可以帮助我加快UI的运行速度?

      Is there any silverlight control that can help me make the UI faster?

      我可以以编程方式定义数据注释吗?例如,在解析自定义属性后,是否可以向该属性添加一些数据注释(显示,验证)并将其绑定到DataForm,PropertyGrid或针对这种情况的有用控件?

      Can I define data annotations programmatically? for example after parsing the custom property, can I add some data annotations (Display, Validation) to the property and bind it to a DataForm, PropertyGrid or a useful control for this situation?

      谢谢.

      推荐答案

      在这种情况下,您通常使用直接从ItemsControl(例如ListBox)或ItemsControl继承的控件之一.从ItemsControl继承的控件允许您为集合中的每个项目定义模板,例如使用示例(假设您可以通过视图模型访问TestClass)

      In these cases you usualy use one of the controls inheriting from ItemsControl (e.g. ListBox) or the ItemsControl directly. The controls inheriting from ItemsControl allow you to define a template for each item in a collection, e.g. using your sample (assuming you got access to your TestClass through a view model):

      <ListBox ItemsSource="{Binding TestClass.CustomProperties }">
          <ListBox.ItemContainerStyle>
              <Style TargetType="ListBoxItem">
                  <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
              </Style>
          </ListBox.ItemContainerStyle>
          <ListBox.ItemTemplate>
              <DataTemplate>
                  <!--DataContext is stet to item in the ItemsSource (of type CustomProperty)-->
                  <StackPanel>
                      <TextBlock Text="{Binding DisplayName}"/>
                      <TextBox Text="{Binding Value}"/>
                  </StackPanel>
              </DataTemplate>
          </ListBox.ItemTemplate>
      </ListBox>
      

      此代码段创建一个ListBox,其中每个CustomProperties集合中的每个CustonProperty都包含一个标签和一个文本框.

      This snippet creates a ListBox that contains a label and a text box for each CustonProperty in your CustomProperties collection.

      这篇关于根据MVVM模式动态创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆