R – How to access silverlight3 DataGrid cell control

celldatagridsilverlight-3.0silverlight-toolkit

how to access silverlight3 DataGrid cell value programatically?

I know that I can use DataContext to access the data, but I need to access control contained in a specific cell.

If column template is like this:

                    <data:DataGridTemplateColumn Header="Header text">
                        <data:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox TextAlignment="Right" x:Name="myTxt" Text="{Binding Path=Val1, Mode=TwoWay}" TextWrapping="Wrap" Width="50" HorizontalAlignment="Left"/>
                            </DataTemplate>
                        </data:DataGridTemplateColumn.CellTemplate>
                    </data:DataGridTemplateColumn>

how can I get a reference to myTxt control?

Thank you

Best Answer

You can use this to examine the visual tree:

    private void GetVisualTreeChildren(DependencyObject element, int depth)
    {
        string spacer = new string(' ', depth * 2);
        System.Diagnostics.Debug.WriteLine(spacer + element.GetType().ToString());
        TextBox txt = element as TextBox;
        if (txt != null)
        {
            ...
        }

        int childCount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < childCount; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(element, i);
            GetVisualTreeChildren(child, depth + 1);
        }
    }

Maybe you can adapt it to what you need?

Related Topic