首页 新闻 会员 周边

WPF DataGrid 如何绑定图片列

1
悬赏园豆:50 [已解决问题] 解决于 2016-01-14 10:24

我现在绑定了图片列 可是界面 上就是显示不出来

请问我哪里出了问题呢?

代码如下:

前端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;
        }
    }
需要格局的主页 需要格局 | 老鸟四级 | 园豆:2145
提问于:2016-01-13 15:59
< >
分享
最佳答案
1

去掉using (System.IO.MemoryStream ms = new System.IO.MemoryStream(img))这里的using

收获园豆:50
jello chen | 大侠五级 |园豆:7336 | 2016-01-13 23:59

我不理解,但是 照你的方法确定有效,你能告诉 我为什么么?

需要格局 | 园豆:2145 (老鸟四级) | 2016-01-14 08:42

@田麦成: 出了using块之后,MemoryStream就close了,然后你的BitmapImage就读不到了

jello chen | 园豆:7336 (大侠五级) | 2016-01-14 12:06

@jello chen:我想问一下,那MemoryStream不用关闭了么?

需要格局 | 园豆:2145 (老鸟四级) | 2016-01-14 13:36

@田麦成: 当你的Image的Source被释放了,它就有机会被GC

jello chen | 园豆:7336 (大侠五级) | 2016-01-14 14:17
其他回答(2)
0

你打下断点看看datatable有数据没 还有那个转换器 也要进断点 

s_p | 园豆:138 (初学一级) | 2016-01-13 16:23

datatable 有数据的,那里的逻辑没写错

支持(0) 反对(0) 需要格局 | 园豆:2145 (老鸟四级) | 2016-01-13 17:31
0

Image的Source可以使用BitmapImage吗?

于为源 | 园豆:956 (小虾三级) | 2016-01-13 17:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册