0

這是怎麼看我的列表框:WP7 - 顯示在列表框中的一些項目按鈕,點擊後顯示更多的內容

<ListBox x:Name="ForthListBox" 
      Margin="0,0,-12,0" 
      ItemsSource="{Binding Tops}" 
      Tap="ForthListBox_Tap" Style="{StaticResource TopListBoxStyle}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Margin="0,0,0,17"> 
         <TextBlock Text="{Binding Title}" 
           TextWrapping="Wrap" 
           Margin="12,0,0,0" 
           FontSize="40"/> 
        <TextBlock Text="{Binding Rating}" 
           TextWrapping="NoWrap" 
           Margin="12,-6,0,0" 
           Style="{StaticResource PhoneTextSubtleStyle}"/> 
        </StackPanel> 
       </DataTemplate> 
     </ListBox.ItemTemplate> 
</ListBox> 

我編輯的列表框模板,所以我在最後有按鈕:

<Style x:Key="TopListBoxStyle" TargetType="ListBox"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="BorderThickness" Value="0"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="Padding" Value="0"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBox"> 
        <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}"> 
         <StackPanel><ItemsPresenter/> 
          <Button x:Name="BtnLoadMore" Click="BtnLoadMore_Click" Content="Další" /> 
         </StackPanel> 
        </ScrollViewer> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

並且我在MainViewModel中有ObservableCollection<Top>(),我將它綁定到DataContext。沒關係,它顯示我的項目,但我怎樣才能設置,我想在該列表框中顯示50個項目,並且當我點擊按鈕後,我想從集合中再次顯示前50個項目和50個項目,並且一次又一次地在收藏中沒有更多項目沒有顯示,然後隱藏按鈕。謝謝

+0

我想,我會收集2本。一個用於綁定,我只是添加我想要顯示的項目,第二個將有所有項目,當我點擊按鈕時從中獲得項目,但我認爲這個解決方案不是最好的。我希望{Binding}中有一些選項 – 2012-03-26 14:00:52

回答

0

你可以在你的ViewModel中有一個變量來跟蹤按鈕被點擊的次數。然後在ObservableCollection的getter中,可以使用LINQ和Take運算符。事情是這樣的:

public class MainPageViewModel : INotifyPropertyChanged { 
     private ObservableCollection<string> _myList; 
     private int _clickCount; 
     private const int ItemsPerPage = 25; 

     public MainPageViewModel() { 
      _myList = new ObservableCollection<string>(); 
      _clickCount = 1; 
      PopulateList(); 
     } 

     private void PopulateList() { 
      for (int i = 0; i < 100; i++) { 
       _myList.Add(string.Format("item {0}", i)); 
      } 
     } 

     public ObservableCollection<string> TheList { 
      get { return new ObservableCollection<string>(_myList.Take(_clickCount * ItemsPerPage).ToList()); } 
     } 

     public void IncrementClickCount() { 
      if (_clickCount * ItemsPerPage < _myList.Count) { 
       _clickCount += 1; 
       RaisePropertyChanged("TheList"); 
      } 
     } 

     protected void RaisePropertyChanged(string property) { 
      if(PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
    } 

然後在MainPage.xaml.cs中

public partial class MainPage : PhoneApplicationPage { 
     private MainPageViewModel _vm; 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 
      Loaded += (s, e) => { 
          _vm = new MainPageViewModel(); 
          DataContext = _vm; 
         }; 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      _vm.IncrementClickCount(); 
     } 
    } 

而就在你的綁定列表框的屬性稱爲的thelist

希望幫助

2

能夠使用隱藏按鈕下面的代碼

private void BtnLoadMore_Click(object sender, RoutedEventArgs e) 
{ 
    AddMoreItems(); 
    Button b = sender as Button; 
    b.Visibility = System.Windows.Visibility.Collapsed; 
} 
相關問題