Search code examples
windows-phone-7xamlsilverlight-4.0

Getting the values of TextBox from a data bound ListBox


I need to get the values from Listbox selected items. Note that, the data templates are in data bound. here is the xaml:

<ListBox Name="AppointmentResultsData" ItemsSource="{Binding}" Height="650" Width="480" Margin="24,0,0,0" Foreground="#CBF7FA" SelectionChanged="AppointmentResultsData_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding Path=Subject, Mode=TwoWay}" TextWrapping="Wrap" FontSize="30" Grid.Column="0" Grid.Row="1"/>
                            <Grid Grid.Column="0" Grid.Row="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <TextBlock Text="{Binding Path=Account.Name}" Grid.Column="0" Grid.Row="1" FontSize="28"/>
                                <TextBlock Text="Start : " Grid.Column="0" FontSize="22" Grid.Row="2"/>
                                <TextBlock Text="{Binding Path=StartTime}" FontSize="22" Grid.Column="1" Grid.Row="2"/>
                                <TextBlock Text="End : " Grid.Column="0" Grid.Row="3" FontSize="22"/>
                                <TextBlock Text="{Binding Path=EndTime}" Grid.Column="1" FontSize="22" Grid.Row="3"/>
                                <TextBlock Text="Location : " Grid.Column="0" Grid.Row="4" FontSize="22"/>
                                <TextBlock Text="{Binding Path=Location}" Grid.Column="1" FontSize="22" Grid.Row="4"/>
                                <TextBlock Text="Status : " Grid.Column="0" FontSize="22" Grid.Row="5"/>
                                <TextBlock Text="{Binding Path=Status}" Grid.Column="1" FontSize="22" Grid.Row="5"/>
                            </Grid>
                            <TextBlock Text="    "/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

I need values of the textboxes in selection changed event.I have tried like this...

private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //SelectedEvent seleted = AppointmentResultsData.SelectedItem as SelectedEvent;
        if (AppointmentResultsData.SelectedIndex == -1)
            return;

        ListBoxItem currentSelectedListBoxItem = this.AppointmentResultsData.ItemContainerGenerator.ContainerFromIndex(AppointmentResultsData.SelectedIndex) as ListBoxItem;

        if (currentSelectedListBoxItem == null)
            return;

        // Iterate whole listbox tree and search for this items
        TextBox nameBox = helperClass.FindDescendant<TextBox>(currentSelectedListBoxItem);
        TextBlock nameBlock = helperClass.FindDescendant<TextBlock>(currentSelectedListBoxItem);
        MessageBox.Show(nameBlock.Text + " " + nameBox.Text);
    }

But it didn't work !


Solution

  • Solved it !

    private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBoxItem = AppointmentResultsData.ItemContainerGenerator.ContainerFromIndex(AppointmentResultsData.SelectedIndex) as ListBoxItem;
            var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtLocation");
    
            MessageBox.Show(txtBlk.Text);
        }
    
    
    
    
    T FindVisualChildByType<T>(DependencyObject element, String name) where T : class
        {
            if (element is T && (element as FrameworkElement).Name == name)
                return element as T;
            int childcount = VisualTreeHelper.GetChildrenCount(element);
            for (int i = 0; i < childcount; i++)
            {
                T childElement = FindVisualChildByType<T>(VisualTreeHelper.GetChild(element, i), name);
                if (childElement != null)
                    return childElement;
            }
            return null;
        }