我需要在另一个中添加一个ObservableCollection ObservableCollection,是否可能?
Items = new ObservableCollection<WidgetCollectionItem>();
foreach (XElement wid in document.Root.Elements("widget"))
{
WidgetCollectionItem item = new WidgetCollectionItem();
item.nombreWidget = wid.Attribute("caption").Value;
foreach (XElement service in wid.Elements("service"))
{
item.nombreServicio = service.Attribute("caption").Value;
item.valor = service.Element("xvalue").Value;
item.color = service.Element("xcolor").Value;
item.alerta = service.Element("xalert") != null ? service.Element("xalert").Value : null;
Items.Add(item);
}
}
问题是每个(item.nombreWidget)包含多个(item.nombreServicio)。我需要在nombreWidget的ObservableCollection中使用ObservableCollection服务
答案 0 :(得分:0)
是的,你可以。
new ObservableCollection<ObservableCollection<WidgetCollectionItem>>()
所以你可以这样做:
Items = new ObservableCollection<ObservableCollection<WidgetCollectionItem>>();
foreach (XElement wid in document.Root.Elements("widget"))
{
ObservableCollection<WidgetCollectionItem> services = new ObservableCollection<WidgetCollectionItem>();
foreach (XElement service in wid.Elements("service"))
{
WidgetCollectionItem widget = new WidgetCollectionItem();
widget.nombreWidget = wid.Attribute("caption").Value;
widget.nombreServicio = service.Attribute("caption").Value;
widget.valor = service.Element("xvalue").Value;
widget.color = service.Element("xcolor").Value;
widget.alerta = service.Element("xalert") != null ? service.Element("xalert").Value : null;
services.Add(widget);
}
Items.Add(services)
}
答案 1 :(得分:0)
最终结果是:
nombreWidget1 nombreServicio1
nombreWidget2 nombreServicio2 nombreServicio2 nombreServicio2
如果nombreWidget只有一个服务没问题,但如果你有多个nombreWidget nombreServicio,我就无法在屏幕上显示
我需要ObservableCollection
public class WidgetCollectionItem
{
public string nombreWidget { get; set; }
}
包含ObservableCollection
public class WidgetServiciosCollectionItem
{
public string nombreServicio { get; set; }
public string valor { get; set; }
public string color { get; set; }
public string alerta { get; set; }
}
答案 2 :(得分:0)
看起来我得到了我想要达到的目标,但仍然没有向我展示网格中的所有数据:
早期的解决方案:
Items = new ObservableCollection<WidgetCollectionItem>();
foreach (XElement wid in document.Root.Elements("widget"))
{
WidgetCollectionItem widget = new WidgetCollectionItem();
widget.nombreWidget = wid.Attribute("caption").Value;
foreach (XElement service in wid.Elements("service"))
{
ServiciosWidgetCollectionItem ser = new ServiciosWidgetCollectionItem();
widget.ItemsSer = new ObservableCollection<ServiciosWidgetCollectionItem>();
ser.nombreServicio = service.Attribute("caption").Value;
ser.valor = service.Element("xvalue").Value;
ser.color = service.Element("xcolor").Value;
ser.alerta = service.Element("xalert") != null ? service.Element("xalert").Value : null;
widget.ItemsSer.Add(ser);
}
Items.Add(widget);
}
public class WidgetCollectionItem
{
public string nombreWidget { get; set; }
public ObservableCollection<ServiciosWidgetCollectionItem> ItemsSer { get; set; }
}
public class ServiciosWidgetCollectionItem
{
public string nombreServicio { get; set; }
public string valor { get; set; }
public string color { get; set; }
public string alerta { get; set; }
}
我如何进入数据网格ObservableCollection?