none
datagrid cell custom rendering RRS feed

  • Question

  • in non-wpf, eg like classic asp.net 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" ... >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Field01}" Header="#" />
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Canvas Name="uiImg" />
                            </DataTemplate>
    

    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

Answers

  • https://stackoverflow.com/a/44761080/782132

    this managed allow me to access the template item.

    uiLayers.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
    
            private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
            {
                // https://stackoverflow.com/questions/44760674/binding-an-event-to-a-control-created-with-a-data-template-in-wpf
    
                var icg = sender as ItemContainerGenerator;
                switch(icg.Status)
                {
                    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
                        }
                        break;
                }
            }
    


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