-1

I try to access x:Name="NomeTextBox", but in code behind dont find. Help me , pliz

        <StackPanel Grid.Row="2" Grid.Column="0" x:Name="nomeToVisible" Visibility="Collapsed">
        <ItemsControl ItemsSource="{Binding ListaCliente}"  >
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock FontSize="15" TextWrapping="Wrap" Text="Nome" Margin="10,0,40,0"/>
                        <TextBox x:Name="NomeTextBox" FontSize="25" VerticalContentAlignment="Bottom" Height="40" Text="{Binding Nome}" TextChanged="TextBox_TextChanged" Margin="10,0,0,0"></TextBox>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
  • Actually [for data template](https://stackoverflow.com/q/16375375/1997232). – Sinatr Jul 28 '21 at 12:28
  • Be aware that there is one TextBox per item. Which one is supposed to be referenced by a possible x:Name? Please tell us why you think you need to access it in code behind. – Clemens Jul 28 '21 at 12:30
  • Clemns - I need its value to save in the database – BrunoTourinho Jul 28 '21 at 12:37
  • That value is in the `Nome` property of the `ListaCliente` elements. You don't need to react on the TextChanged event. The `TextBox.Text` Binding is TwoWay by default, but you may need to add `UpdateSourceTrigger=PropertyChanged` to get the changed text into the source property immediately, instead of the default `LostFocus` behaviour. – Clemens Jul 28 '21 at 12:58
  • I still can't access the TextBox value, I really don't know what to do. I made the changes you said , but nothing has changed – BrunoTourinho Jul 28 '21 at 13:22
  • You should not access the TextBox. It is not necessary and bad practice. Instead, access the Nome property of the item object, as already said. – Clemens Jul 28 '21 at 15:19

1 Answers1

-1

you can have access to that textbox in the event "TextChanged" like this

    private void NomeTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    var textbox = (TextBox)sender;

    var obj = textbox.DataContext;
}

Here you get access to the control and the object inside of it.

u can make it also in the selection changed event of the item control to get the data context of your custom object LISTACLIENTE.

Dharman
  • 30,962
  • 25
  • 85
  • 135