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
40
Getting to the control from active record
posted

 

How do I get a reference to the TextBox with ID="textModified" from the ActiveRecord in the following data grid:

 

<infg:XamDataGrid Name="dataGrid" DataSource="{Binding Tokens}" AutoFit="True" RecordActivated="OnRecordActivated" GroupByAreaLocation="None" Background="White" KeyUp="dataGrid_KeyUp">
                <infg:XamDataGrid.FieldLayoutSettings>
                    <infg:FieldLayoutSettings HighlightAlternateRecords="True" AutoGenerateFields="False" />
                </infg:XamDataGrid.FieldLayoutSettings>
                <infg:XamDataGrid.FieldLayouts>
                    <infg:FieldLayout>                        
                            <infg:Field Name="ModifiedText" Label="Modified Text">
                                <infg:Field.Settings>
                                    <infg:FieldSettings CellValuePresenterStyle="{StaticResource TokenModifiedText}" />
                                </infg:Field.Settings>
                            </infg:Field>
                        </infg:FieldLayout.Fields>
                    </infg:FieldLayout>
                </infg:XamDataGrid.FieldLayouts>
            </infg:XamDataGrid>

<Style TargetType="{x:Type infg:CellValuePresenter}" x:Key="TokenModifiedText">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type infg:CellValuePresenter}">
                        <Grid Width="{TemplateBinding Width}">
                            <TextBox Name="textModified" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 

Thank you!

  • 2677
    posted

    Hello mmorozov,

    In the RecordActivated event(or any event as long as there is an active row) you can get the cellValuePresenter from a cell using the static method off the CellValuePresenter class.  Once you have the CellValuePresenter, you can use the VisualTreeHelper.GetChild() and GetChildrenCount methods to find the child that matches your criteria which is based on the name and type of object.  I have pasted some code below.

    private void xamDataGrid1_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
            {
                CellValuePresenter cvp = CellValuePresenter.FromCell(((DataRecord)e.Record).Cells[0]);
                DependencyObject dobj = FindChild(cvp,"textModified",typeof(TextBox));
                if (dobj != null)
                {
                    TextBox tb = (TextBox)dobj;
                    tb.Text = "I found it";
                }

            }

            public DependencyObject FindChild(DependencyObject reference, string childName, Type childType)
            {
                DependencyObject foundChild = null;
                if (reference != null)
                {
                    int childrenCount = VisualTreeHelper.GetChildrenCount(reference);
                    for (int i = 0; i < childrenCount; i++)
                    {
                        var child = VisualTreeHelper.GetChild(reference, i);
                        // If the child is not of the request child type child
                        if (child.GetType() != childType)
                        {
                            // recursively drill down the tree
                            foundChild = FindChild(child, childName, childType);
                        }
                        else if (!string.IsNullOrEmpty(childName))
                        {
                            var frameworkElement = child as FrameworkElement;
                            // If the child's name is set for search
                            if (frameworkElement != null && frameworkElement.Name == childName)
                            {
                                // if the child's name is of the request name
                                foundChild = child;
                                break;
                            }
                        }
                        else
                        {
                            // child element found.
                            foundChild = child;
                            break;
                        }
                    }
                }
                return foundChild;
            }

    This should do the trick.  Have a good day!  Any problems with this, let me know.