[英]how to access TextBox inside ListBox itemsSource which is Datatemplate?
I have a ListBox
and its ItemsSource
is linked with a ComboBox
SelectedItem
. 我有一个ListBox
,它的ItemsSource
与ComboBox
SelectedItem
链接。 Its template is associated with a DataTemplate
. 其模板与DataTemplate
关联。 Everything is fine but how to access each TextBox
inside ListBoxItems
. 一切都很好,但是如何访问ListBoxItems
每个TextBox
。 I have 5 labels and 2 TextBoxes
inside each ListItem
. 我在每个ListItem
有5个标签和2个TextBoxes
。 I want to access each and every TextBox
and labels inside ListBoxItem
. 我想访问ListBoxItem
每个TextBox
和标签。 I need some idea how to access each TextBox
inside each Item. 我需要一些想法如何访问每个项目内的每个TextBox
。 For example, there is "wbprofileDesc" TextBox
in first ListBoxItem
. 例如,第一个ListBoxItem
有“ wbprofileDesc” TextBox
。 So I need to access this TextBox
and write some functionality to it like keypress event. 因此,我需要访问此TextBox
并为其编写一些功能,例如keypress事件。 It need to work for each and every TextBox
inside all the ListBoxItems
individually. 它需要为所有ListBoxItems
每个TextBox
单独工作。 Assume there are 5 ListBoxItems
. 假设有5个ListBoxItems
。 Also I need to fetch other controls also like wbselect( ComboBox
), wbdepth, wbwidthvalue and etc. I am using MVVM model for this. 我还需要获取其他控件,如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"/>
Here is an example of you could find the controls in the DataTemplate
inside the event handler: 这是一个示例,您可以在事件处理程序内的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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.