wpfautogeneratecolumn

Using image in bool-type column when using AutoGenerateColumn (WPF)


I have

if (e.PropertyType == typeof(bool))
        {             
           e.Column.Header = "Status";
           DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
           templateColumn.Header = "Status";
           DataTemplate imageTemplate = new DataTemplate();

           DataTrigger imageBoolTrigger = new DataTrigger();
           Converter.BoolToImage boolToImage = new Converter.BoolToImage();

           Uri trueImageURI = new Uri(@"pack://application:,,,/MonitorView_wpf;component/Images/Icons/check-mark-16.png", UriKind.RelativeOrAbsolute);

           boolToImage.TrueImage.Source = new BitmapImage(trueImageURI);

           imageBoolTrigger.Binding = new Binding { Converter = boolToImage, ConverterParameter = e.Column };

           imageTemplate.Triggers.Add(imageBoolTrigger);
           templateColumn.CellTemplate = imageTemplate;

           e.Column = templateColumn;

        }

but alas, its not working.

how do I use an image instead of the autogenerated checkbox?

my converter looks like this:

 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();
        }
    }

my image does exist in the assemby, it's a png - ive tried all sorts of things, but never working.

thanks in advance


Solution

  • Too much behind-code for my liking, personally I would create a template for it in resources:

        <DataGridTemplateColumn x:Key="booleanImageTemplate">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Name="theImage" Width="64" Height="64" Source="true_image.png"/>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding theBooleanPropertyName}" Value="False">
                            <Setter TargetName="theImage" Property="Source" Value="false_image.png"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    

    Then override it on creation:

    <Grid>
        <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" AutoGeneratingColumn="OnAutogeneratingColumn" />
    </Grid>
    
    private void OnAutogeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column.Header.ToString() == "theBooleanPropertyName")
                e.Column = (DataGridTemplateColumn)Resources["booleanImageTemplate"];
        }
    

    I've just used an image here (effectively making the column read only) but it would work equally well with a templated checkbox.