简体   繁体   中英

how to access TextBox inside ListBox itemsSource which is Datatemplate?

I have a ListBox and its ItemsSource is linked with a ComboBox SelectedItem . Its template is associated with a DataTemplate . Everything is fine but how to access each TextBox inside ListBoxItems . I have 5 labels and 2 TextBoxes inside each ListItem . I want to access each and every TextBox and labels inside ListBoxItem . I need some idea how to access each TextBox inside each Item. For example, there is "wbprofileDesc" TextBox in first ListBoxItem . So I need to access this TextBox and write some functionality to it like keypress event. It need to work for each and every TextBox inside all the ListBoxItems individually. Assume there are 5 ListBoxItems . Also I need to fetch other controls also like wbselect( ComboBox ), wbdepth, wbwidthvalue and etc. I am using MVVM model for this.

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

Here is an example of you could find the controls in the DataTemplate inside the event handler:

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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