动态创建控制以下模式。
-
28-10-2019 - |
题
我想要动态产生了一些控制在我的silverlight应用程序。
要更加明确,这里有一个简化的定义是我的类:
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; }
}
每个"包括加"最后一个文本框框,或组合框:
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.
}
什么是最好的方式来实现这一使用。模式?如果我析CustomProperties在视图模型,并找出其控制应该建立,我怎么可以创造新的控制,我认为根据。模式。
是否有任何silverlight控制,可以帮助我做UI更快?
我可以定义的数据注释的编程方式?例如析后的定义酒店,我可以添加一些数据说明(显示,验证)的财产,并结合这一数据的窗体,PropertyGrid或者一个有用的控制这个情况?
谢谢你。
解决方案
在这些情况下通常使用的控件之一的继承 ItemsControl
(例如 ListBox
)或 ItemsControl
直接。的控制继承 ItemsControl
能让你定一个模板中的每个项目收集的,例如用你的样品(假定你有权访问你 TestClass
通过视图模型):
<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
包含一个标签和一个文本框每 CustonProperty
在你 CustomProperties
收集。
不隶属于 StackOverflow