Image inside a DataGrid in a FixedDocument Error

BigH61 541 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.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,562 questions
C#
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.
9,502 questions
{count} votes

Accepted answer
  1. gekka 2,631 Reputation points MVP
    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);
            }
        }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful