如何访问 ListBox itemsSource 中的 TextBox,它是 Datatemplate?

how to access TextBox inside ListBox itemsSource which is Datatemplate?

我有一个 ListBox,它的 ItemsSource 链接到一个 ComboBox SelectedItem。它的模板与 DataTemplate 相关联。一切都很好,但如何访问 ListBoxItems 中的每个 TextBox。我有 5 个标签,每个 ListItem 里面有 2 个 TextBoxes。我想访问 ListBoxItem 中的每个 TextBox 和标签。我需要了解如何访问每个项目中的每个 TextBox。比如第一个ListBoxItem里面有"wbprofileDesc"TextBox。所以我需要访问这个 TextBox 并向它写入一些功能,比如按键事件。它需要单独为所有 ListBoxItems 中的每个 TextBox 工作。假设有 5 ListBoxItems。此外,我还需要获取其他控件,如 wbselect(ComboBox)、wbdepth、wbwidthvalue 等。我为此使用 MVVM 模型。

<Window.Resources>
  <local:wbItemViewModel x:Key="wbItem"/>

  <DataTemplate x:Key="wbObjectsDataTemplate">
    <Grid Grid.ColumnSpan="1" Grid.RowSpan="1" Height="Auto" Width="642" Margin="0,0,0,-14">
      <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Width="697"  Margin="10,0,0,0" Height="54" >
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="49*"/>
          <ColumnDefinition Width="91*"/>
          <ColumnDefinition Width="309*"/>
          <ColumnDefinition Width="306*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="auto" />
          <RowDefinition/>
          <RowDefinition Height="auto" MinHeight="5"/>
        </Grid.RowDefinitions>

        <Label Content="{Binding WBName_lbl}" Margin="0,3,0,5" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"/> 

        <ComboBox x:Name="wbselect" Margin="5,0,10,1" Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="0">
          <ComboBoxItem x:Name="wbstraight" IsSelected="True" Content="straight"/>
          <ComboBoxItem x:Name="wbtapered" Content="tapered"/>
        </ComboBox>

        <!--KeyDown="{Binding Path=profileDesc}"-->
        <!-- KeyDown="profileDesc_KeyDown" -->
        <TextBox x:Name="wbprofileDesc" Margin="18,0,20,1" Grid.Column="2" Grid.Row="0" GotFocus="wbprofileDesc_GotFocus"/>
        <TextBox x:Name="wbdepth" Text="{Binding ElementName=wbwidthvalue, Path=Content, Mode=OneWay}" Margin="10,0,73,1" Grid.Column="3" Grid.Row="0"/>
        <Label x:Name="wbwidthvalue" Margin="10,0,190,5" Grid.Column="2" FontSize="8" Grid.Row="1"/>
        <Label x:Name="wbthicknessvalue" Margin="118,0,82,5" FontSize="8" Grid.Row="1" Grid.Column="2"/>
        <Label x:Name="wblengthvalue" Margin="208,0,0,5" FontSize="8" Grid.Row="1" Grid.Column="2"/>
        <Label x:Name="wbnexwidthvalue" Margin="10,0,178,5" FontSize="8" Grid.Row="1" Grid.Column="3"/>
        <Label x:Name="wbdepthvalue" Grid.Row="1" Grid.Column="3" FontSize="8" Margin="132,0,31,5"/>
        <!--<Label x:Name="totalvalue" Margin="30,10,24,16" Grid.Row="3" Grid.Column="3"/>-->
      </Grid>
    </Grid>
  </DataTemplate>
</Window.Resources>

<ListBox x:Name="wbListDataTemplate"  
         ItemsSource="{Binding wbVisibleItems}"           
         ItemTemplate="{DynamicResource wbObjectsDataTemplate}"
         DataContext="{DynamicResource wbItem}"
         Background="{x:Null}"
         SelectedItem="{Binding wbSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         IsSynchronizedWithCurrentItem="True"
         Canvas.Top="51" Height="222" Width="686"/>

这是您可以在事件处理程序内的 DataTemplate 中找到控件的示例:

private void wbprofileDesc_GotFocus(object sender, RoutedEventArgs e)
{
    TextBox wbprofileDesc = sender as TextBox;
    Grid parentGrid = wbprofileDesc.Parent as Grid;

    ComboBox wbselect = parentGrid.Children.OfType<ComboBox>().FirstOrDefault(x => x.Name == "wbselect");
    Label wbwidthvalue = parentGrid.Children.OfType<Label>().FirstOrDefault(x => x.Name == "wbwidthvalue");
}