2014-10-01 88 views
3

我有一个DataGridTextColumn包含一个布尔值,这些布尔值在列上返回TRUE或FALSE。布尔转换器打印图像,只是打印图像名称c#

我想用两个图像替换它,一个为TRUE,一个为FALSE。

这是在DataGrid:

<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=ResultatCollectionGrande}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected"> 

      <DataGrid.Columns> 
       <DataGridTextColumn x:Name="PrisEnCompte" Width="50" Binding="{Binding Path=Flag, Converter={StaticResource BooleanConverter}}" Header="PEC"></DataGridTextColumn> 

与资源:

<Window.Resources> 
    <local:BooleanConverter x:Key="BooleanConverter"/> 
</Window.Resources> 

这是转换器:

public class BooleanConverter : IValueConverter 
{ 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value.Equals(true)) return @"booleanTrue.png"; 
     return @"booleanFalse.png"; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

转换的作品,它改变了什么我DataGridTextColumn显示器。现在它显示“booleanTrue.png”为TRUE和“booleanFalse.png”,但它不显示图像。

我该如何display'em,我错过了什么以在我的DataGrid中打印这些图像?

在此先感谢您的帮助。

回答

1

除了使用DataGridTextColumn之外,还需要使用Template列来在此列中显示Image,并仅在此模板列中应用您的绑定。让我给你带路

你的DataGrid的应该是这样的

<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=ResultatCollectionGrande}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected"> 
<DataGrid.Columns> 
    <DataGridTemplateColumn Header="PEC" x:Name="PrisEnCompte"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Image Height="50" Width="50" Source="{Binding Path=Flag, Converter={StaticResource booleanConverter}}"></Image> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

,你的布尔转换器应该是这样的。

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value.Equals(true)) 
       return new Uri(@"C:\Users\pj827192\Desktop\Untitled.png"); 
      return new Uri(@"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg"); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
+1

转换器不一定要返回Uris。它也可以是字符串,因为WPF的内置类型转换可以类型化ImageSource(Image控件的'Source'属性的类型)适用于两者。此外,当图像作为程序集资源(我将从OP中假设)存储时,您将不得不返回有效的[资源文件包URI](http://msdn.microsoft.com/zh-cn/library/aa970069。 ASPX#Resource_File_Pack_URIs ___ Local_Assembly)。另一种方法是直接创建并返回BitmapImage或BitmapFrame实例。 – Clemens 2014-10-01 09:32:52

-1

也许你应该返回嵌入图像的标记而不是图像名称。

+0

这些图像是解决方案的根源,所以在之前添加'\',是什么意思? – Flo 2014-10-01 08:51:10

+0

不,我的意思是返回一个字符串,如: ”alternate Pieter21 2014-10-01 08:59:06

+0

是否有可能在DataGrid.Columns上返回像这样的字符串?我会尽力。 **编辑**似乎是不可能的,它只需要DataGrid复选框/组合框/超链接/模板/文本也许一个'em可以让我做我想做的? – Flo 2014-10-01 09:00:56