从成员数量未知的列表动态构建选择列表
我想做的是创建一个 silevrlight 弹出窗口,向用户显示图像,并让他们选择下面的无线电控件来确定他们选择的选项。我有一个这样的对象:
public class ConfigImage
{
public int ConfigID { get; set; }
public string ConfigName { get; set; }
public string ImagePath { get; set; }
}
代码返回一个包含未知数量成员的 ConfigImage 列表。我尝试使用网格向用户显示图像,因此我根据列表中的成员数量动态添加列。我预计名单中会有 2 至 5 名成员。我遇到的问题是尝试动态添加图像和无线电控件。我似乎无法在任何地方找到这方面的例子。我尝试使用如下代码添加控件:
LayoutRoot.Children.Add(new Label);
但随后不知道如何在新的 Label 控件上设置属性。我应该知道这一点,但我一片空白,似乎找不到它的例子。
非常感谢您的帮助!
What I am trying to do is create a silevrlight popup that displays images to a user and lets them select a radio control underneath to determine which option they have selected. I have an object like this:
public class ConfigImage
{
public int ConfigID { get; set; }
public string ConfigName { get; set; }
public string ImagePath { get; set; }
}
The code returns a list of ConfigImage with an unknown number of members. I am trying to use a grid to display the images to the user, so I dynamically add columns based on the number of members in the list. I expect anywhere from 2-5 members to be in the list. What I am having the problem with is trying to dynamically add the image and radio controls. I cannot seem to find an example anywhere of this. I tried to add controls using code such as this:
LayoutRoot.Children.Add(new Label);
but then have no idea how to set properties on the new Label control. I should know this, but I am drawing blank and cannot seem to find an example of it.
Help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您绝对必须在代码中添加控件,则需要引用该对象才能在其上设置属性:
或者,您可以使用初始值设定项:
正如 BrokenGlass 所说,您可以完全在 xaml 中完成此操作,不过。
编辑:使用 BrokenGlass 建议的 ItemsControl 来说明仅使用 xaml 的方法:
If you absolutely have to add the controls in code, you will need to have a reference to the object in order to set properties on it:
Alternatively, you can use initializers:
As BrokenGlass said, you can probably do this completely in xaml, though.
Edit: To illustrate the xaml-only approach using BrokenGlass's suggestion of ItemsControl:
只需使用 Silverlight 中任何基于列表的 UI 元素 - 这些元素将允许您将数据绑定到可以在运行时更新的可观察集合,最简单的一个是 ItemsControl - 您可以完全控制的集合中的每个项目使用什么标记XAML。
just use any of the list based UI elements in Silverlight - those would allow you to data bind to an observable collection that you can update at runtime, simplest one would be ItemsControl - what markup you use for each item in the collection you can completely control in XAML.