2011-04-07 91 views
0

我正在嘗試創建一個向用戶顯示圖像的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控件上設置屬性。我應該知道這一點,但我畫空白,似乎無法找到它的一個例子。

幫助將不勝感激!

回答

0

如果你絕對必須添加代碼的控制,你將必須以它的屬性設置爲對象的引用:或者

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

,你可以使用初始化:

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

正如BrokenGlass所說,儘管你可以在xaml中完全做到這一點。

編輯:說明使用的ItemsControl的BrokenGlass的建議只有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> 
0

只需使用任何基於在Silverlight UI元素列表 - 那些將允許您將數據綁定到一個可觀察您可以在運行時更新的集合,最簡單的一個是ItemsControl - 您可以在集合中的每個項目上使用什麼標記,您可以在XAML中完全控制這些標記。