2011-04-29 78 views
2

我想要一個列表框,允許用戶從數據庫中讀取20個項目,並在列表框的最後一行顯示一個提示,如果有更多項目需要提取。當用戶點擊最後一行時,應從數據庫中檢索其他項目,直到沒有更多項目,最後一行顯示此信息。WPF列表框顯示最後一個項目不同

第一:

listitem1 
listitem2 
... 
listitem19 
listitem20 
Button: <get_more> 

後按下按鈕:

listitem1 
listitem2 
... 
listitem39 
listitem40 
Info: <no more items> 

難道這一切只在XAML來完成? 什麼是最好的解決方案來實現這個?

回答

1

多德 - 一切可以用XAML來完成:d

繼MVVM的做法,我建議你做以下幾點:

1 /入門:一個DockPanel

<DockPanel LastChildFill="True"> 
    <Button DockPanel.Dock="Bottom" /> 
    <ListBox /> 
</DockPanel> 

2 /綁定你ListBox到您的視圖模型的ObservableCollection

<ListBox ItemsSource="{Binding ListElements}" /> 

在視圖模型:

private ObservableCollection<String> _listElements; 

     public ObservableCollection<String> ListElements 
     { 
      get { return _listElements; } 
      set { _listElements = value; } 
     } 

3 /綁定你Button的內容預定義的String

<Button Content="{Binding ButtonString}" /> 

在視圖模型:

public String ButtonString 
{ 
    get 
    { 
     //There, define if there are any more things to display 
    } 
} 

4 /你Button啓動一個Command啓動一個方法,讓我們來看看AY GetMore()

<Button Content="{Binding ButtonString}" Command="{Binding GetMoreCommand} /> 

在視圖模型:

private void GetMore() 
{ 
    //append to the _listElements new elements from the list 
    //Update the ButtonString if there are no more elements 
} 

而且你去那裏!

(你也可以,如果需要的話,定義一個按鈕從ObservableCollection例如去除的東西)

+0

感謝@Damascus。你的解決方案肯定會工作,但在列表框外面有按鈕有一些缺點:它們都在一個垂直scroollbar的視圖中,在你的情況下,我認爲它會是DockPanel的滾動條。我希望滾動在ListBox上完成,以便例如我可以使用滾輪直接在列表框中滾動。 – mcanti 2011-05-02 05:05:20

+0

您可以爲列表顯示滾動條。這實際上是將按鈕放在ListBox旁邊的目的。只需從dockPanel中移除滾動條(我不確定是否存在'IsScrollable'屬性,但至少可以將'Scrollviewer.VerticalScrollbarVisibility'設置爲'Hidden'來指定'DockPanel',因此列表將處理它 – Damascus 2011-05-02 07:04:48

+0

問題是,我想讓列表框和按鈕在有限的視圖中(讓我們說100個像素)。列表框擴展到可以說1000個像素,然後出現按鈕。我將列表框和按鈕放入StackPanel中,放置在ScrollViewer中。這樣做確實是可以的,但我無法使用鼠標滾輪進行滾動!看起來,鼠標滾輪事件被列表框捕獲(它沒有做任何事情,因爲它被展開),事件不會向上滾動到滾動查看器。也許我應該解決這個問題,而不是尋找其他解決方案:) – mcanti 2011-05-03 08:01:39

相關問題