I have tetbox inside the list box data template. That textbox is editable. How to get the textbox text value in selection changed event. If i tried to find the text using visual tree it returns nothing. Please help
ListBoxItem item = this.lstProds.SelectedItem as ListBoxItem;
PhoneTextBox targetBox = FindFirstElementInVisualTree<PhoneTextBox>(item);
string a = targetBox.Text;
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
XAML:
<ListBox Name="lstProds" Margin="0,59,0,0" Background="{x:Null}" Foreground="Black" SelectionChanged="lstProds_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="auto">
<Border BorderBrush="Black" BorderThickness="1" Margin="68,63,-58,-13">
<toolkit:PhoneTextBox Name="txtQuantity" Hint="Quantity" Width="180" Text="{Binding Quantity}"></toolkit:PhoneTextBox>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I just add the textbox text property mode as Two way. In the selection changed event get the selected item as a class. var context = (lstProds.SelectedItem as ModelClass); string a = context.Quantity;
<ListBox Name="lstProds" Margin="0,59,0,0" Background="{x:Null}" Foreground="Black" SelectionChanged="lstProds_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Border BorderBrush="Black" BorderThickness="1" Margin="91,69,0,9" HorizontalAlignment="Left" Width="145">
<toolkit:PhoneTextBox Name="txtQuantity" Hint="Quantity" Width="144" Text="{Binding Quantity,Mode=TwoWay}"></toolkit:PhoneTextBox>
</Border>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
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.