zoukankan      html  css  js  c++  java
  • wpf DataGrid列中绑定图片删除

    wpf界面

    <Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MainWindw.CommHelper">

     <DataGridTemplateColumn Header="图片" MinWidth="150">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Image Height="100" Width="100"  Source="{Binding Path=FilePath, Converter={StaticResource ImagePathConverter}}" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
    
    
     <Window.Resources>
            <local:ConvertTextToImage  x:Key="ImagePathConverter"   />
        </Window.Resources>
    
    

    c#代码

    public   class ConvertTextToImage : IValueConverter
    {
           #region IValueConverter Members
           public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
           {
               if (value == null)
                   return null;
     
               if (!string.IsNullOrEmpty(value.ToString()))
               {
                   BitmapImage bi = new BitmapImage();
                   bi.BeginInit();
                   bi.UriSource = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);
                   bi.CacheOption = BitmapCacheOption.OnLoad;
                   bi.EndInit();
                   return bi;
    }
     
               return null;
           }
           public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
           {
               throw new NotImplementedException();
           }
           #endregion
    }
     

    在按钮中删除绑定的图片时候 不会报错

    该图片不能删除,因为别的进程正在使用它

    如果程序报错

    WPF, binding to an image without file access exceptions(绑定的图像文件访问异常)

    可能是该文件不是图像格式文件造成。文件格式必须是png,jpg之类的图像格式才行。

  • 相关阅读:
    CodeForces1152CNeko does Maths
    π的计算公式
    IFS变量对加双引号和不加双引号变量的区别对待
    各种距离
    ADO.NET Data Service 二 绑定数据类
    向客户端注入JavaScript代码
    Ajax ModelPopu and Progress 示例学习
    Sliverlight 入门教程七
    (牛人莫入)Jquery plugin 多文件上传
    自定义控件的验证使用
  • 原文地址:https://www.cnblogs.com/z_lb/p/2980987.html
Copyright © 2011-2022 走看看