How to reference a Control in DataGridTemplateColumn

Feb 12, 2009 at 2:03 AM
Edited Feb 12, 2009 at 7:17 AM
Hi,

I am trying to create a DataGrid that is able to display images in some columns so I decided to use DataGridTemplateColumn to achieve this capability.

I am generating columns based on data from the database. So I decided to use the folowing template as Cell Template of my special column.

    <DataTemplate x:Key="ImageTemplateColumn">
        <Image x:Name="Flag" Source="{Binding Path=Image}" />
    </DataTemplate>

Everything works fine.

OnLoading RowDetials I am loading images and binding to the Image control. I am doing this manualy because I want to be able to change and/or delete the displayed Image while it is displayed in the grid, but it is another story.
Everything works fine when the row details are shown, but the cells with Images in the main row are empty.
These cells are filled with images by Data Binding  only when I am scrolling row by row.   (This means that everything is OK with DataGrid Column Templating.)

So I decided to add some functionallity in the LoadingRow event handler of the DataGrid. The problem is, I can't handle how to reach the child Image control of the Templated column form that location.
I have to load and bind Images when the row is loaded.

Has anybody some solution for my problem ?

P.S.  I  tried to find Logical or Visual Children but unsuccesfuly. No children was found.
Feb 12, 2009 at 7:22 AM
The piece of code in Loading RowDetails that fills the Image Source property  I am using is:

In the RowDetial for displaying images I placed a Button which has changed ControlTemplate where I placed the Image so it looks like
an Image with Click capability.
...

            Image BoundImage = null;
            Button[] childs = VisualTreeUtility.FindChildVisuals<Button>(CurrentRowDetail);
            foreach (Button child in childs)
            {
                if (child is Button)
                {
                    ControlTemplate template = child.Template;
                    object control = template.FindName("img", child);
                    if (control is Image)
                    {
                        string propertyName = "";
                        Image imgControl = control as Image;
                        BoundImage = imgControl;
                        try
                        {
                            string s = (string)imgControl.Tag;
                            string[] split = s.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                            string BindingPath = "";
                            int i = 0;
                            foreach (string sp in split)
                            {
                                switch (i)
                                {
                                    case 0:
                                        {
                                            // Path
                                            break;
                                        }
                                    case 1:
                                        {
                                            BindingPath = sp;
                                            //
                                            break;
                                        }
                                }
                                i++;
                            }
                            propertyName = BindingPath;
                        }
                        catch
                        {
                        }

                        try
                        {
                            PropertyInfo propInfo = dataCollection.RecordType.GetProperty(propertyName);
                            if (propInfo == null)
                            {
                                Logger.error(string.Format("Undefined property ({0}) .", propertyName));
                                return;
                            }
                            string path = (string)propInfo.GetValue(record, null);
                            if (path != null && path != string.Empty)
                            {
                                BitmapImage bi = SetImage(path, UriKind.Absolute);
                                BoundImage.Source = bi;

                            }
                            else
                            {
                                BoundImage.Source = null;
                            }
                        }
                        catch(Exception ex)
                        {
                            Logger.error(ex);
                        }
                    }
                }
            }