wpf datagrid field to image converter 字段 图片转化器
1 <DataGridTemplateColumn Header="Binding"> 2 <DataGridTemplateColumn.CellTemplate> 3 <DataTemplate> 4 <Image x:Name="BindImg" Height="30" Width="30" Source="{Binding IsBindable,Converter={StaticResource bindableConverter}}" VerticalAlignment="Center" HorizontalAlignment="Center"/> 5 </DataTemplate> 6 </DataGridTemplateColumn.CellTemplate> 7 </DataGridTemplateColumn>
1 public class BindableConverter : IValueConverter 2 { 3 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 4 { 5 bool isBind; 6 if(value==null||string.IsNullOrWhiteSpace(value.ToString())) 7 { 8 return "../../Resource/Images/unbinded.jpg"; 9 } 10 if (Boolean.TryParse(value.ToString(), out isBind)) 11 { 12 if (isBind) 13 { 14 return "../../Resource/Images/binded.jpg"; 15 } 16 else 17 { 18 return "../../Resource/Images/unbinded.jpg"; 19 } 20 } 21 return null; 22 } 23 24 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 25 { 26 throw new NotImplementedException(); 27 } 28 }