您的位置:首页 > 其它

wpf DataGrid列中绑定图片删除

2013-03-25 16:25 381 查看
wpf界面

<Windowx: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">

<DataGridTemplateColumnHeader="图片"MinWidth="150">

<DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<ImageHeight="100"Width="100"Source="{BindingPath=FilePath,Converter={StaticResourceImagePathConverter}}"/>

</DataTemplate>

</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>



<Window.Resources>

<local:ConvertTextToImagex:Key="ImagePathConverter"/>

</Window.Resources>



c#代码

publicclassConvertTextToImage:IValueConverter

{

#regionIValueConverterMembers

publicobjectConvert(objectvalue,TypetargetType,objectparameter,System.Globalization.CultureInfoculture)

{

if(value==null)

returnnull;


if(!string.IsNullOrEmpty(value.ToString()))

{

BitmapImagebi=newBitmapImage();

bi.BeginInit();

bi.UriSource=newUri(value.ToString(),UriKind.RelativeOrAbsolute);

bi.CacheOption=BitmapCacheOption.OnLoad;

bi.EndInit();

returnbi;

}


returnnull;

}

publicobjectConvertBack(objectvalue,TypetargetType,objectparameter,System.Globalization.CultureInfoculture)

{

thrownewNotImplementedException();

}

#endregion

}



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

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

如果程序报错

WPF,bindingtoanimagewithoutfileaccessexceptions(绑定的图像文件访问异常)

可能是该文件不是图像格式文件造成。文件格式必须是png,jpg之类的图像格式才行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: