3

I am new in WPF, so I think it's a simple question. The problem is that I cannot get the textbox name. The textbox is inside the GridView. Is there any solution for it? The textbox is under the DataTemplate.

XAML

<GridViewColumn.CellTemplate >
    <DataTemplate >
        <StackPanel >
            <TextBox x:Name="txtPurchasePrice" 
                     Width="60" 
                     TextChanged="txtPurchasePrice_TextChanged" />
        </StackPanel>
    </DataTemplate>
</GridViewColumn.CellTemplate>
Xaruth
  • 4,034
  • 3
  • 19
  • 26
  • 1
    Could this be what you are looking for? http://stackoverflow.com/questions/16997951/how-to-access-datagrid-template-column-textbox-text-wpf-c-sharp – Karl-Henrik Mar 11 '14 at 07:52
  • You can not get textbox directly into codebehind if it is in grid view you need to first select a raw from grid view and then find textbox from that raw. – Manish Parakhiya Mar 11 '14 at 07:52

1 Answers1

2

Unfortunately, there's way to do this as simple as accessing a named object.One option would be to iterate through the child objects of the parent control and check the text fields against a known valu

private void Button_Click(object sender, RoutedEventArgs e)
{
    GetTextBox(datagrid);
}

public void GetTextBox(Visual visual)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
    {
        Visual childVisual = (Visual) VisualTreeHelper.GetChild(visual, i);
        if (childVisual is TextBox)
            MessageBox.Show("textbox Found");
        // Recursively enumerate children of the child visual object.
    }
}
Xaruth
  • 4,034
  • 3
  • 19
  • 26
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396