2017-06-05 55 views
0

我有一个ListBox和它的ItemsSource链接ComboBoxSelectedItem。其模板与DataTemplate相关联。一切都很好,但如何访问ListBoxItems内的每个TextBox。我在每个ListItem内有5个标签和2个TextBoxes。我想访问每个TextBoxListBoxItem内的标签。我需要一些想法如何访问每个项目内的每个TextBox。例如,在第一个ListBoxItem中有“wbprofileDesc”TextBox。所以我需要访问这个TextBox并写一些功能,如按键事件。它需要分别在所有ListBoxItems内部的每个TextBox工作。假设有5个ListBoxItems。此外我需要获取其他控件,如wbselect(ComboBox),wbdepth,wbwidthvalue等。我正在使用MVVM模型。如何访问ListBox中的TextBox itemsSource是Datatemplate?

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

是什么原因?如果您实际上是“使用MVVM”,则不需要访问ItemTemplate中的元素。 – Clemens

+0

我正在使用Combobox并根据它的选定值ListItems需要更改。所以使用MVVM。 –

+1

DataTemplate中的ItemsControl没有任何意义。它只有一个项目,即网格。这不是ItemsControl应该如何使用的。 – Clemens

回答

1

下面是一个例子可以发现在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"); 
} 
+0

这个相同的代码可以用于按钮吗?一次处理所有项目。 –

+0

如果您有其他问题,请提出一个新问题。 – mm8

相关问题