一佳一

记录像1+1一样简洁的代码

导航

WPF ListBox 自动排列

Posted on 2013-03-21 11:45  一佳一  阅读(5753)  评论(0编辑  收藏  举报

网上一堆ListBox代码,但是不是只能横向就是只能纵向,没办法,只好自己写一个,希望能帮到大家,废话不多说 直接上代码

前台:

<Window x:Class="WpfApplication1.showpifu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="showpifu" Height="400" Width="490" Loaded="Window_Loaded">
<Grid>
<StackPanel>
<ListBox x:Name="lstImgs" Height="350">
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled">
<WrapPanel Orientation="Horizontal" IsItemsHost="True">
</WrapPanel>
</ScrollViewer>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,0" BorderBrush="Black" Width="150" Height="100">
<Image MouseMove="Image_MouseMove" MouseLeave="Image_MouseLeave" Stretch="Fill" Source="{Binding Path=FullPath}"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
</Window>

 

后台:

/// <summary>
/// showpifu.xaml 的交互逻辑
/// </summary>
public partial class showpifu : Window
{
public showpifu()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{

DataTable pic = new DataTable();
pic.Columns.Add("FullPath");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
pic.Rows.Add("http://img.kuuke.com/kuuke/doing/201211/20_23_06_46_230.jpg");
this.lstImgs.ItemsSource = pic.DefaultView;

}
private void Image_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
Image image = sender as Image;
Border border = image.Parent as Border;
border.BorderThickness = new Thickness(2, 2, 2, 2);
}

private void Image_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
Image image = sender as Image;
Border border = image.Parent as Border;
border.BorderThickness = new Thickness(0, 0, 0, 0);
}
}

代码比较简单,我就不说明了,看不懂的再留言问我吧.

这东西看简单,其实真的还是用了很多自己的时间,转载的童鞋,记得保留我的连接http://www.cnblogs.com/linyijia/archive/2013/03/21/2973012.html,不做纯粹的伸手党哦!