我想在同一个集合中保存不同类型的对象,但无法解决问题。
public class SetupStep
{
public ObservableCollection</*Picture- or ParameterContaienr*/> Containers {get; set;}
}
public class PictureContainer
{
public ObservableCollection<Picture> Content {get; set;}
}
public class ParameterContainer
{
public ObservableCollection<Parameter> Content {get; set;}
}
我认为这可以通过接口(interface)和泛型来解决,但到目前为止我还没有找到可行的解决方案。 最后,容器集合将绑定(bind)到 WPF 中 ScatterView 的 ItemsSource,它应该显示具有不同内容的各个容器。
请您参考如下方法:
让他们实现相同的接口(interface):
public interface IContainer
{
// common properties
}
public class Picture: IContainer
{
// prop
}
public class Parameter: IContainer
{
//prop
}
public class SetupStep
{
public ObservableCollection<IContainer> Containers {get; set;}
}
如何填充您的收藏:
SetupStep step = new SetupStep();
step.Containers = new ObservableCollection<IContainer>();
Parameter p = new Parameter();
Picture pic = new Picture();
step.Containers.Add(p);
step.Containers.Add(pic);
