您可以使用 DataTemplate 根据 Value 属性类型选择不同的 View。
看法:
<ItemsControl ItemsSource="{Binding Path=Options}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<DataTemplate DataType="{x:Type System:Boolean}">
<CheckBox IsChecked="{Binding Path=.}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type System:String}">
<TextBox Text="{Binding Path=.}"/>
</DataTemplate>
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name, Mode=OneWay}"/>
<ContentControl Content="{Binding Path=Value}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
视图模型:
public class MainViewModel
{
public ArrayList Options { get; set; }
public MainViewModel()
{
Options = new ArrayList();
Options.Add(new TextProperty());
Options.Add(new BoolProperty());
}
}
public class TextProperty
{
public string Name { get; set; }
public string Value { get; set; }
public TextProperty()
{
Name = "Name";
Value = "Default";
}
}
public class BoolProperty
{
public string Name { get; set; }
public bool Value { get; set; }
public BoolProperty()
{
Name = "IsEnabled";
Value = true;
}
}