简体   繁体   English

x:Name 在 c# wpf 后面的代码中找不到,我如何获得它的值?

[英]x:Name dont find in code behind c# wpf, how do i get the value of it?

I try to access x:Name="NomeTextBox", but in code behind dont find.我尝试访问 x:Name="NomeTextBox",但在后面的代码中找不到。 Help me , pliz帮帮我,plz

        <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>

you can have access to that textbox in the event "TextChanged" like this您可以像这样在“TextChanged”事件中访问该文本框

    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.您也可以在项目控件的选择更改事件中使用它来获取自定义对象 LISTACLIENTE 的数据上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM