界面
<WindowsFormsHost Grid.Row="1" Grid.RowSpan="2" Opacity="1" FontSize="14">
<wf:TreeView x:Name="tree" AfterSelect="tree_AfterSelect"></wf:TreeView>
</WindowsFormsHost>
<ListBox x:Name="listBox" Grid.Row="1" Grid.Column="1">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem x:Name="upload" Header="下载" Click="upload_Click"></MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid x:Name="ufg" Columns="4"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<StackPanel Grid.Row="2" Grid.Column="1" Style="{StaticResource StackPanelStyle}" HorizontalAlignment="Center">
<Button Content="First" x:Name="btnFirst" Style="{StaticResource ButtonStyle}" Click="btnFirst_Click"></Button>
<Button Content="Previous" x:Name="btnPrev" Style="{StaticResource ButtonStyle}" Click="btnPrev_Click"></Button>
<Button Content="Next" x:Name="btnNext" Style="{StaticResource ButtonStyle}" Click="btnNext_Click"></Button>
<Button Content="Last" x:Name="btnLast" Style="{StaticResource ButtonStyle}" Click="btnLast_Click"></Button>
<TextBlock x:Name="tbindex" Style="{StaticResource TextBlockStyle}" Margin="10,5,5,5" FlowDirection="RightToLeft"></TextBlock>
<TextBlock Text="/" Style="{StaticResource TextBlockStyle}" Width="Auto"></TextBlock>
<TextBlock x:Name="tbmax" Style="{StaticResource TextBlockStyle}" HorizontalAlignment="Left"></TextBlock>
</StackPanel>
代码
List<ListBoxItem> ListListBoxItem;
int index = 0;
int pagesize = 16;
int tbindexnum = 1;
int tbmaxnum;
//tree点击
private void tree_AfterSelect(object sender, TreeViewEventArgs e)
{
listBox.ItemsSource = ListLbi(pagesize, e.Node.Text);
}
//右键下载
private void upload_Click(object sender, RoutedEventArgs e)
{
ListBoxItem lb = listBox.SelectedItem as ListBoxItem;
StackPanel sp = (StackPanel)lb.Content;
System.Windows.Controls.Label lable = (System.Windows.Controls.Label)sp.Children[1];
System.Windows.MessageBox.Show(lable.Content.ToString());
}
//首页
private void btnFirst_Click(object sender, RoutedEventArgs e)
{
tbindex.Text = 1.ToString();
tbindexnum = 1;
index = 0;
listBox.ItemsSource = ListListBoxItem.Skip(0).Take(pagesize).ToList();
}
//尾页
private void btnLast_Click(object sender, RoutedEventArgs e)
{
tbindex.Text = tbmaxnum.ToString();
tbindexnum = tbmaxnum;
index = (tbmaxnum - 1) * pagesize;
listBox.ItemsSource = ListListBoxItem.Skip(index).Take(pagesize).ToList();
}
//上一页
private void btnPrev_Click(object sender, RoutedEventArgs e)
{
if (tbindexnum > 1)
{
tbindex.Text = (--tbindexnum).ToString();
index -= 16;
listBox.ItemsSource = ListListBoxItem.Skip(index).Take(pagesize).ToList();
}
else
{
System.Windows.MessageBox.Show("已经到最前了");
}
}
//下一页
private void btnNext_Click(object sender, RoutedEventArgs e)
{
if (tbindexnum < tbmaxnum)
{
tbindex.Text = (++tbindexnum).ToString();
index += 16;
listBox.ItemsSource = ListListBoxItem.Skip(index).Take(pagesize).ToList();
}
else
{
System.Windows.MessageBox.Show("已经到最后了");
}
}
//加载tree
public void LoadTree()
{
TreeNode root = new TreeNode("全部");
TreeNode child = new TreeNode("分类");
TreeNode childNode = new TreeNode("子类");
child.Nodes.Add(childNode);
root.Nodes.Add(child);
tree.Nodes.Add(root);
}
//ListBox添加item
public List<ListBoxItem> ListLbi(int pagesize, string str)
{
ListListBoxItem = new List<ListBoxItem>();
Test test = new Test();
List<Test> list = test.ReturnTest();
int listLength = list.Count;
for (int i = 0; i < listLength; i++)
{
ListBoxItem lbi = new ListBoxItem();
lbi.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
StackPanel sp = new StackPanel();
Image image = new Image();
image.Source = new BitmapImage(new Uri(list[i].image, UriKind.Relative));
System.Windows.Controls.Label lable = new System.Windows.Controls.Label();
lable.Content = str + i;
sp.Children.Add(image);
sp.Children.Add(lable);
lbi.Content = sp;
ListListBoxItem.Add(lbi);
}
if (listLength % pagesize == 0)
{
tbmaxnum = listLength / pagesize;
}
else
{
tbmaxnum = listLength / pagesize + 1;
}
tbindex.Text = 1.ToString();
tbmax.Text = tbmaxnum.ToString();
index = 0;
tbindexnum = 1;
return ListListBoxItem.Skip(0).Take(pagesize).ToList();
}
如图