Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get textbox name in wpf

Tags:

c#

wpf

textbox

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>

1 Answers

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.
    }
}
like image 84
Sajeetharan Avatar answered Mar 18 '26 00:03

Sajeetharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!