tags:

views:

64

answers:

2

i have a column in a datagrid that the content is True/false, how can i change this true/false(boolean) to a image, according to the text?

I'm using c# wpf.

Edit:

<dg:DataGridTemplateColumn  MinWidth="70" Header=" Is Done2">
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Name="imgIsDone" Source="../Resources/Activo.png"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsDone}" Value="False">
                    <Setter TargetName="imgIsDone" Property="Source" Value="../Resources/Inactivo.png"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
+3  A: 

Use a DataGridTemplateColumn to supply a DataTemplate for the column that contains an Image, and use a value converter or a data trigger to set the image source based on the value of the column. Here is an example that uses a data trigger:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Name="MyImage" Source="TrueImage.png"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding BoolColumn}" Value="False">
                    <Setter TargetName="MyImage" Property="Source" Value="FalseImage.png"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Quartermeister
just edit the topic, to the code i have, and gives me a error.first say that: the file... is not partof the project or its 'build action' is not set to resource..when i run it, the line <DataTrigger Binding="{Binding BoolColumn}" Value="False"> says that 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '64' and line position '46'.
Luis
+2  A: 
public class BoolToImage : IValueConverter 
{
    public Image TrueImage { get; set; }
    public Image FalseImage { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!(value is bool))
        {
            return null;
        }

        bool b = (bool)value;
        if (b)
        {
            return this.TrueImage            }
        else
        {
            return this.FalseImage
        }
    }

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

Then in your xaml, as a resource:

<local:BoolToImage TrueImage="{StaticResource Image}" FalseImage="{StaticResource FalseImage}" x:Key="BoolImageConverter"/>

Then in your binding:

ImageSource={Binding Path=BoolProp,Converter={StaticResource BoolImageConverter}}"

benPearce