2011-03-22 39 views
0

我有以下XAML:問題的Silverlight結合

... 
<ListBox Name ="RoomsListBox" Height="100" 
HorizontalAlignment="Left" Margin="12,41,0,0" 
VerticalAlignment="Top" Width="120"></ListBox> 
... 

而下面的C#-code:

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    RoomsListBox.ItemsSource = new[] { new { Name = "First1" }, 
     new { Name = "First2" } }; 
    RoomsListBox.DisplayMemberPath = "Name"; 
} 

的問題是,我的列表框有項目,但它們是空的。爲什麼我沒有看到「First1」和「First2」?

回答

1

這裏的問題是不是與綁定,也不ItemTemplate中也不是變化的通知。這是您正在使用的Anonymous Type導致它。嘗試使用類或結構爲您的項目

public class Item 
{ 
    public string Name { get; set; } 
} 

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    RoomsListBox.ItemsSource = new[] { 
             new Item { Name = "First1" }, 
             new Item { Name = "First2" }}; 
    RoomsListBox.DisplayMemberPath = "Name"; 
} 

XAML中保持不變,或者如果你願意,你可以定義一個DataTemplate爲ListBox項目。請注意,您不能同時設置ItemTemplateDisplayMemberPath(其中一個必須爲空)。此外,請確保代表您項目的班級必須公開。

希望這會有所幫助:)

+0

它確實有幫助。謝謝。我已經使用了一個單獨的課程,但問題依然存在。這是因爲匿名課程和我新創建的課程是私人的而不是公開的。我只是想知道爲什麼沒有關於它的警告或例外? – Peter17 2011-03-22 13:19:33

+0

這裏的問題是數據綁定機制無法訪問您的類,因此它對此一無所知(也不知道如何綁定它)!缺少警告,異常和最重要的「調試」是DataBinding一般的問題!這會隨着Silverlight 5的發佈而改變,它將引入「DataBinding Debugging」,這就是COOOL :) – AbdouMoumen 2011-03-22 13:31:36

+0

感謝您對Silverlight 5的解釋和好消息:) – Peter17 2011-03-22 13:42:58

0

您必須將您的ListBox上的DisplayMemberPath屬性設置爲Name

展望未來,你可能要考慮爲您創造一個項目到DataTemplate有更多的控制:

<ListBox x:Name ="RoomsListBox" Height="100" 
HorizontalAlignment="Left" Margin="12,41,0,0" 
VerticalAlignment="Top" Width="120"> 
    <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding Name}"/> 
       </StackPanel> 
      </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

請參見本教程的詳細信息:http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-5-using-the-listbox-and-databinding-to-display-list-data.aspx

+0

我在我的代碼中設置了該屬性。 – Peter17 2011-03-22 10:08:14

+0

對不起,錯過了,正在xaml中尋找它。新增了一個DataTemplate的例子,你可以使用它,我認爲這是一個更好的解決方案! – dain 2011-03-22 10:27:26

+0

我改變了我的XAML。但我仍然有一個空物品清單。我不明白如何可能。默認的ToString()方法永遠不會返回空字符串。 – Peter17 2011-03-22 10:39:02

0

只是一個想法..

您是否嘗試過在XAML中設置DisplayMemberPath屬性?通話順序可能存在問題。

0

我寧願你在你的xaml中定義你的綁定,例如在你的代碼背後爲你的列表框的項目定義一個屬性。

例子:(XAML)

<ListBox Name ="RoomsListBox" 
     ItemsSource="{Binding MyItems}" 
     Height="100" 
     HorizontalAlignment="Left" 
     Margin="12,41,0,0" 
     VerticalAlignment="Top" 
     Width="120" /> 

實例:(C#中的代碼隱藏)

//... 
private ObservableCollection<string> _myItems; 

public ObservableCollection<String> MyItems 
{ 
    get 
    { 
     return _myItems ?? (_myItems = new ObservableCollection<string> { "FirstItem", "SecondItem"}); 
    } 
    set 
    { 
     _myItems = value; 
    } 
} 

就像ChrisF說,你可以使用INotifiyPropertyChanged接口,那裏你將提高PropertyChanged事件在你的財產的二傳手。

查看 - >http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

+0

我已經刪除了我的答案,反思(通過評論),我不認爲它在這種情況下是相關的。除非OP按照這些方向重寫他的代碼。 – ChrisF 2011-03-22 12:27:21