Share via

Image inside a DataGrid in a FixedDocument Error

BigH61 581 Reputation points
2023-09-28T12:40:04.9533333+00:00

I have created this small demonstration project to highlight a problem I have encountered when working with an Image within a DataGrid created in C# and then utilised within a FixedDocument.

Everything appears to work as expected until the mouse goes over an Image, at which time an error occurs “System.ArgumentNullException: 'Value cannot be null. Arg_ParamName_Name'”

If there is no image in the in the Image column cell then no issues.

https://1drv.ms/u/s!AuEmZJuOJ6_bgctd-Fgbkv_JqUaFzw?e=aQW5AS

My ultimate aim is to extract the image from a database as a byte[] and add this to the DataGrid but the same error occurs and therefore solution for one I hope will be a solution for both. My DataGrid will also almost certainly extend over multiple sheets (paper size may also vary) therefore I was creating the bespoke DataGrid's for each fixed page in c# rather than xaml.

I hope someone can help resolve this issue.

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Answer accepted by question author

gekka 14,146 Reputation points MVP Volunteer Moderator
2023-09-29T09:14:48.48+00:00

It' a Bug in WPF.

This bug can be avoided by hiding the ImageControl from the mouse.

public void DataGrid()
{
    StampModel stampModel = new StampModel();
    Type type = stampModel.GetType();
    foreach (var property in type.GetProperties())
    {
        if (property.Name != "ImageSource")
        {
            CreateGridColumn(property.Name);
        }
        else
        {
            DataGridTemplateColumn ImageCol = new DataGridTemplateColumn();
            ImageCol.Header = "Image";
            ImageCol.Width = 120;
            ImageCol.IsReadOnly = true;

            FrameworkElementFactory imageFactory = new FrameworkElementFactory(typeof(Image));
            imageFactory.SetBinding(Image.SourceProperty, new Binding("ImageSource"));
                    
            FrameworkElementFactory gridFactoryInner = new FrameworkElementFactory(typeof(Grid));
            gridFactoryInner.SetValue(Grid.BackgroundProperty, Brushes.Transparent);

            FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
            gridFactory.AppendChild(imageFactory);
            gridFactory.AppendChild(gridFactoryInner);                                

            DataTemplate dataTemplate = new DataTemplate();
            dataTemplate.VisualTree = gridFactory;
            //<DataTemplate>
            //    <Grid>
            //        <Image Source="{Binding Path=ImageSource}" />
            //        <Grid Background="Transparent" x:Name="dummyGrid"/>
            //    </Grid>
            //</DataTemplate>

            ImageCol.CellTemplate = dataTemplate;

            Style ImageColumnStyle = new Style();
            ImageColumnStyle.TargetType = typeof(DataGridCell);

            ImageCol.CellStyle = ImageColumnStyle;
            dataGrid.Columns.Add(ImageCol);
        }
    }

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.