我有一个休息服务,其中有一个组列表,每个组都有一个 GroupName,在我的客户端,我试图将这些 GroupName 添加到变量 groupbox 的列表中(组框的数量取决于我的休息组服务中有多少 GroupName ) 任何人都可以帮助编写代码吗?:
string uriGroups = "http://localhost:8000/Service/Group";
XDocument xDoc = XDocument.Load(uriGroups);
var groups = xDoc.Descendants("Group")
.Select(n => new
{
GroupBox groupbox = new GroupBox();
groupbox.Header = String.Format("Group #{0}", n.Element("GroupName");
groupbox.Width = 100;
groupbox.Height = 100;
groupbox.Margin = new Thickness(2);
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(groupbox);
stackPanel.Margin = new Thickness(10);
MainArea.Children.Add(stackPanel);
}
这是不正确的,我只是坚持如何去做。
编辑:
public Reports()
{
InitializeComponent();
string uriGroups = "http://localhost:8000/Service/Group";
XDocument xDoc = XDocument.Load(uriGroups);
foreach(var node in xDoc.Descendants("Group"))
{
GroupBox groupbox = new GroupBox();
groupbox.Header = String.Format("Group #{0}", node.Element("Name"));
groupbox.Width = 100;
groupbox.Height = 100;
groupbox.Margin = new Thickness(2);
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(groupbox);
stackPanel.Margin = new Thickness(10);
MainArea.Children.Add(stackPanel);
}
}
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
{
foreach (var item in enumerable)
action(item);
return enumerable;
}