datagrid cell custom rendering RRS feed

  • Question

  • in non-wpf, eg like classic webform, I can use code like DataGridRow.FindName("UI_Element") to access an ui object and play around with it

    i have tried similar approach in WPF DataGrid, supposed to access the row via VisualTreeHelper with custom library code, but my problem is VisualTreeHelper.GetChildCount(DataGridRow_instance_or_whatever_i_tried) keep yielding me zero count.

    Googling basically show more stuffs about virtualization, also few mentioned WPF design this in totally different way whereby my mentioned approach is not working along its design.

    I have this XAML:

            <DataGrid Name="uiLayers" ... >
                    <DataGridTextColumn Binding="{Binding Field01}" Header="#" />
                                <Canvas Name="uiImg" />

    I want to code stuffs that dynamically drawing graphic (basic shapes) in the canvas (or whatever in WPF I can draw/render simple graphic)

    seems to me the VisualTreeHelper is not the right approach for WPF, can anyone show me how to do this, or just telling the proper words/terms related what I intend to do so i can try googling.

    tried: wpf data template

    Tuesday, March 13, 2018 9:53 AM



    this managed allow me to access the template item.

    uiLayers.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
            private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
                var icg = sender as ItemContainerGenerator;
                    case System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated:
                        foreach(var item in icg.Items)
                            var container = icg.ContainerFromItem(item) as DataGridRow;
                            var uiImg = Utils.FindChild<Canvas>(container, "uiImg"); // FindChild custom code using VisualTreeHelper
                            if (uiImg == null) continue;
                            // doing stuffs here

    • Marked as answer by Kelmen Wednesday, March 14, 2018 8:23 AM
    Wednesday, March 14, 2018 8:23 AM