DataGrid简单数据绑定实例2
1.Image列显示:
后台绑定:
//获取文件夹下的图片 string path = @"K:\Picture\jpg"; private void Button_Click(object sender, RoutedEventArgs e) { List<FileInfo> list = new List<FileInfo>(); if (Directory.Exists(path)) { string[] fiels = Directory.GetFiles(path); foreach (string item in fiels) { list.Add(new FileInfo(item)); } } gridOne.ItemsSource = list; }
前台设置:
<DataGrid Name="gridOne" Margin="10" Grid.Row="1" ColumnWidth="100" AlternatingRowBackground="LightGoldenrodYellow" AutoGenerateColumns="False" > <DataGrid.Columns> <DataGridTextColumn Header="文件名称" Binding="{Binding Name}"></DataGridTextColumn> <DataGridTextColumn Header="扩展名" Binding="{Binding Extension}" IsReadOnly="True"></DataGridTextColumn> <DataGridTextColumn Header="文件夹名称" Binding="{Binding DirectoryName}" IsReadOnly="True"></DataGridTextColumn> <DataGridTextColumn Header="文件大小" Binding="{Binding Length}"></DataGridTextColumn> <DataGridTextColumn Header="创建时间" Binding="{Binding CreationTime}"></DataGridTextColumn> <DataGridCheckBoxColumn Header="是否只读" Binding="{Binding IsReadOnly}"></DataGridCheckBoxColumn> <DataGridTemplateColumn Header="Image" Width="SizeToCells" MinWidth="50" IsReadOnly="True"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Image Height="80" Source="{Binding FullName}"></Image> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
显示结果: