Image inside a DataGrid in a FixedDocument Error

BigH61 541 Reputation points

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.!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
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

    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")
                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));
                DataTemplate dataTemplate = new DataTemplate();
                dataTemplate.VisualTree = gridFactory;
                //    <Grid>
                //        <Image Source="{Binding Path=ImageSource}" />
                //        <Grid Background="Transparent" x:Name="dummyGrid"/>
                //    </Grid>
                ImageCol.CellTemplate = dataTemplate;
                Style ImageColumnStyle = new Style();
                ImageColumnStyle.TargetType = typeof(DataGridCell);
                ImageCol.CellStyle = ImageColumnStyle;
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful