【发布时间】:2012-09-28 14:45:56
【问题描述】:
我想绑定对象列表,每个对象都有 UIType(即文本框、复选框、组合等)。并且它具有对象 Value 属性。
我想将此列表绑定到一个 ItemControl(ListBox、DataGrid..),其中每个项目将具有与每个对象的特定 UIType 对应的单独模板(例如,组合项目将在行中具有组合,复选框项目将有复选框)...
显然 prop Value 将绑定到每个项目的相关属性。
实现此目的最透明且不太复杂的方法是什么?
银光 5.
编辑:(基于 Jacob 解决方案的工作代码)
代码:
ObservableCollection<UIType> data;
public MainPage()
{
InitializeComponent();
data = new ObservableCollection<UIType>()
{
new UITypeTextBox() { Value = "Value.." },
new UITypeCheckBox(){ Value = true },
};
lb.ItemsSource = data;
}
public class UIType { }
public class UITypeTextBox : UIType { public object Value { get; set; }}
public class UITypeCheckBox : UIType { public object Value { get; set; } }
xaml:
<ListBox x:Name="lb">
<ListBox.Resources>
<DataTemplate DataType="local:UITypeTextBox">
<TextBox Text="{Binding Value}" />
</DataTemplate>
<DataTemplate DataType="local:UITypeCheckBox">
<CheckBox IsChecked="{Binding Value}" />
</DataTemplate>
</ListBox.Resources>
</ListBox>
【问题讨论】:
标签: c# wpf silverlight .net-4.0