2010-01-14 90 views
0

我在我的Silverlight 3應用程序中有一個簡單的ComboBox。我想從ObservableCollection填充它。該列表包含具有Name(字符串)和Selected(布爾)屬性的類。組合框包含的項目與列表中的項目數量一樣多,但我似乎無法獲取列表數據。Silverlight 3組合框ItemTemplate綁定

任何幫助,將不勝感激。

<ComboBox x:Name="cmbCategory" Grid.Column="3"> 
<ComboBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Name}"/> 
      <CheckBox IsChecked="{Binding Selected}"/> 
     </StackPanel> 
    </DataTemplate> 
</ComboBox.ItemTemplate> 

... 
private class cmbCategoryClass 
    { 
     public string Name { get; set; } 
     public bool Selected { get; set; } 
    } 
    private ObservableCollection<cmbCategoryClass> _categories; 
.... 
cmbCategory.DataContext = _categories; 
cmbCategory.ItemsSource = _categories; 

回答

0

我無法從你的代碼告訴我們,如果這是一個代碼隱藏或視圖模型。我猜你實際上正在填充代碼中的_categories列表,以便它至少包含一個cmbCategoryClass對象。嘗試刪除將DataContext設置爲_categories的行,因爲您的ItemsSource可能正在DataContext上查找_categories屬性在調試模式下運行時,請檢查Visual Studio中的Output窗口,以找出數據綁定失敗的線索。