Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2589
Cannot find a control inside cell
posted

Hello,

In CellControlAttached event I try to find control inside cell. Control is defined in DataTemplate:

 

<DataTemplate x:Key="colItemTemplate">

            <Grid x:Name="TemplateContent">

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="Auto"/>

                    <ColumnDefinition/>

                </Grid.ColumnDefinitions>

                <Controls1:Alert x:Name="AlertSign" Visibility="Collapsed"/>

                <TextBlock Text="{Binding Path=Text}" TextWrapping="Wrap" Margin="2" MinWidth="80" Grid.Column="1"/>

            </Grid>

        </DataTemplate>

This is what I do:

Alert el = (Alert)e.Cell.Control.FindControlByName("AlertSign");
if (el == null) return;

And this is a method which search for control:

public static FrameworkElement FindControlByName(this FrameworkElement element, string name) 

        {

            if (element == null) return null;

            if (element.Name == name) return element;

            FrameworkElement foundElement = null;

 

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)

            {

                FrameworkElement visual = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

                foundElement = visual.FindControlByName(name);

                if (foundElement != null)

                    break;

            }

            return foundElement;

        }

When grid loads first time and CellControlAttached fires , VisualTreeHelper.GetChildrenCount returns 0. When I refresh grid and fire CellControlAttached again, I can find control.

Can you please tell me what is the problem?

Thank you