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; 
} 
相关问题