我现在绑定了图片列 可是界面 上就是显示不出来
请问我哪里出了问题呢?
代码如下:
前端XAMAL如下:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfApplication1" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="750" Width="750" Loaded="Window_Loaded"> <Window.Resources> <local:DateConverter x:Key="dateConverter"></local:DateConverter> </Window.Resources> <Grid> <DataGrid x:Name="ASPNET" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="38,36,0,0" VerticalAlignment="Top" Height="607" Width="607" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="名字" Width="150" Binding="{Binding Path=Name}"></DataGridTextColumn> <DataGridTextColumn Header="年龄" Width="150" Binding="{Binding Path=Age}"></DataGridTextColumn> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Image Width="150" Height="150" Source="{Binding Path=PHOTO,Converter={StaticResource dateConverter}, Mode=OneWay}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <!--<DataGridTextColumn Width="150" Header="发送时间" Binding="{Binding AddTime,Mode=OneWay,StringFormat='yyyy-MM-dd HH:mm:ss'}" CellStyle="{StaticResource dgvCellLeft10}"/>--> </DataGrid.Columns> </DataGrid> </Grid> </Window>
后台的代码如下:
private void Window_Loaded(object sender, RoutedEventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Age", typeof(string)); dt.Columns.Add("PHOTO", typeof(byte[])); for (int i = 0; i < 3; i++) { byte[] imageBuffer = File.ReadAllBytes("C:\\Users\\Administrator\\Desktop\\IOS\\Index.png"); dt.Rows.Add("AAAA", "BBBB", imageBuffer); } this.ASPNET.DataContext = dt; //this.ASPNET.ItemsSource = dt.DefaultView; }
转换器如下:
ValueConversion(typeof(byte[]), typeof(BitmapImage))] public class DateConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { byte[] img = null; if (!string.IsNullOrEmpty(value.ToString())) { img = (byte[])value; } if (img == null) { return "/HDMSWpfManage;component/templet/images/defaulthead.jpg"; } return ShowSelectedIMG(img); //以流的方式显示图片的方法 } //转换器中二进制转化为BitmapImage datagrid绑定仙石的 private BitmapImage ShowSelectedIMG(byte[] img) { if (img != null) { //img是从数据库中读取出来的字节数组 using (System.IO.MemoryStream ms = new System.IO.MemoryStream(img)) { ms.Seek(0, System.IO.SeekOrigin.Begin); BitmapImage newBitmapImage = new BitmapImage(); newBitmapImage.BeginInit(); newBitmapImage.StreamSource = ms; newBitmapImage.EndInit(); return newBitmapImage; } } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return null; } }
去掉using (System.IO.MemoryStream ms = new System.IO.MemoryStream(img))这里的using
我不理解,但是 照你的方法确定有效,你能告诉 我为什么么?
@田麦成: 出了using块之后,MemoryStream就close了,然后你的BitmapImage就读不到了
@jello chen:我想问一下,那MemoryStream不用关闭了么?
@田麦成: 当你的Image的Source被释放了,它就有机会被GC
你打下断点看看datatable有数据没 还有那个转换器 也要进断点
datatable 有数据的,那里的逻辑没写错
Image的Source可以使用BitmapImage吗?