简体   繁体   English

从成员数未知的列表中动态构建选择列表

[英]Dynamically build a selection list from a list with an unknown number of members

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. 我想要做的是创建一个silevrlight弹出窗口,该弹出窗口向用户显示图像,并让他们在下面选择一个无线电控件,以确定他们选择了哪个选项。 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. 该代码将返回一个ConfigImage列表,其中包含未知数量的成员。 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. 我希望清单中有2-5位成员。 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. 但是不知道如何在新的Label控件上设置属性。 I should know this, but I am drawing blank and cannot seem to find an example of it. 我应该知道这一点,但是我很空白,似乎找不到一个例子。

Help would be much appreciated! 帮助将不胜感激!

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: 如果绝对必须在代码中添加控件,则需要对对象进行引用才能在其上设置属性:

Label label = new Label();
label.Content = "text";
label.Width = 10;
LayoutRoot.Children.Add(label);

Alternatively, you can use initializers: 另外,您可以使用初始化程序:

LayoutRoot.Children.Add(new Label()
{
    Content = "text",
    Width = 10
});

As BrokenGlass said, you can probably do this completely in xaml, though. 正如BrokenGlass所说,您可能可以完全在xaml中完成此操作。

Edit: To illustrate the xaml-only approach using BrokenGlass's suggestion of ItemsControl: 编辑:为了说明使用BrokenGlass的ItemsControl建议的仅xaml方法:

<ItemsControl x:Name="ConfigImagesItemsControl" ItemsSource="MyConfigImagesList">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Image Grid.Row="0" Source="{Binding ImagePath}" />
                <RadioButton Grid.Row="1" Content="{Binding ConfigName}" GroupName="ConfigImagesGroup" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

只需在Silverlight中使用任何基于列表的UI元素-这些元素将允许您将数据绑定到可在运行时更新的可观察集合,最简单的一个将是ItemsControl-您可以完全控制集合中每个项目使用的标记在XAML中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM