2011-03-31 116 views
0

我想知道如何填充列表框的Dinamically。但我不想要一個自定義類,我只想要一個普通的選擇器。填充ListBoxItem內容

我常做這樣的事情我自己的類:

<ListBox Name="itemList" SelectionChanged="itemList_SelectionChanged"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <TextBlock Text="{Binding title}"/> 
       <TextBlock Text="{Binding subtitle}" /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

我不知道的是如何簡單地生成最簡單的事:這樣的事情:

<ListBox> 
    <ListBoxItem Name="item1" Content="First item" /> 
    <ListBoxItem Name="item2" Content="Second item" /> 
    <ListBoxItem Name="item3" Content="Third item" /> 
</ListBox> 

莫非你給出一個結構的例子?我不知道有沒有用的DataTemplate還是什麼......

謝謝

[編輯:在我所需要的加入Name屬性]

回答

2

你只需要建立一個集合您的項目在您的DataContext並將其設置爲ListBox.ItemsSource。然後這將填寫您的DataTemplate。

見例如:

<Grid> 
    <Grid.Resources> 
     <src:Customers x:Key="customers"/> 
    </Grid.Resources> 
    <ListBox ItemsSource="{StaticResource customers}" Width="250" Margin="0,5,0,10" 
     DisplayMemberPath="LastName"/> 
</Grid> 

和:

public class Customer 
{ 
    public String FirstName { get; set; } 
    public String LastName { get; set; } 
    public String Address { get; set; } 

    public Customer(String firstName, String lastName, String address) 
    { 
     this.FirstName = firstName; 
     this.LastName = lastName; 
     this.Address = address; 
    } 

} 

public class Customers : ObservableCollection<Customer> 
{ 
    public Customers() 
    { 
     Add(new Customer("Michael", "Anderberg", 
       "12 North Third Street, Apartment 45")); 
     Add(new Customer("Chris", "Ashton", 
       "34 West Fifth Street, Apartment 67")); 
     Add(new Customer("Cassie", "Hicks", 
       "56 East Seventh Street, Apartment 89")); 
     Add(new Customer("Guido", "Pica", 
       "78 South Ninth Street, Apartment 10")); 
    } 

} 

示例源:http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource(v=vs.95).aspx

編輯:根據意見的討論,也許你只需要一個簡單的ItemsControl(因爲你不需要保留選定的項目,或者甚至更好地處理多個選擇,這是ListBox的目的)。

例如:

<ItemsControl ItemsSource="{Binding NavigateItems}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Button Content="{Binding Label}" 
        Command="{Binding ButtonCommand}"                
        CommandParameter="{Binding URL}"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

和:

public class NavigateItem 
{ 
    public String Label { get; set; } 
    public String URL { get; set; } 

    public NavigateItem(String label, String url) 
    { 
     this.Label = label; 
     this.URL = url; 
    } 

} 

public class NavigateItems : ObservableCollection<NavigateItem> 
{ 
    public NavigateItems() 
    { 
     Add(new NavigateItem("Google", "http://www.google.com"); 
     Add(new NavigateItem("Bing", "http://www.bing.com"); 
    } 
} 

當然搭好ButtonCommand導航到該參數傳遞的網址,但取決於你如何設置ViewModel /綁定。

+0

我覺得這是一個很長的解決方案,如此簡單。那麼我忘了說我也想設置ListBoxItems的name屬性。我想我可以做一些事情,比如在C#中準備一系列listitems並將它們綁定到列表框。這可能嗎? – enkara 2011-03-31 11:15:04

+0

你爲什麼要設置名稱屬性?我懷疑你以後可能想用它來訪問它們? – dain 2011-03-31 11:20:01

+0

是的,也許我完全在錯誤的路徑...我想要一個典型的選擇器(例如城市),但這必須是動態的,我解析XML來獲取列表。這個選擇會觸發一個事件 – enkara 2011-03-31 11:25:24

1

如果您沒有或想要自定義類,即如果您只有一個字符串集合,那麼您可以將該字符串集合(List,IEnumerable,string []等)分配爲ItemsSource對於代碼隱藏中的ListBox(或將其與XAML綁定),它將使用TextBlock在每個ListBoxItem內呈現該內容。

+0

對不起,我忘了我也必須設置名稱屬性 – enkara 2011-03-31 11:16:20