从数据绑定ListBox获取TextBox的值

时间:2012-07-10 11:14:03

标签: windows-phone-7 xaml silverlight-4.0

我需要从列表框选中的项目中获取值。请注意,数据模板是数据绑定的。这是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>

我需要选择更改事件中文本框的值。我试过这样......

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

但它不起作用!

3 个答案:

答案 0 :(得分:1)

解决了!

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

答案 1 :(得分:0)

假设您有一个类别(MyClass)对象列表,您已将其数据绑定到列表框

将处理程序gesturelistener tap添加到datatemplate

在处理程序中执行此操作:

private void ItemClickedEventHandler(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {                   
           MyClass clickedMyclass = (MyClass)((System.Windows.Controls.Grid)sender).DataContext;

        }

您拥有当前所选项目的对象,您可以访问所有类变量。例如(StartTime等)

答案 2 :(得分:0)

嗯,你把它扔到错误的类型,这应该工作:

private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {  
      var listBoxItem = AppointmentResultsData.SelectedItem as ListBoxItem;
      TextBox nameBox = listBoxItem .FindName("nameYourTextBox") as TextBox;
      TextBlock nameBlock = dd.FindName("nameYourTextBlock") as TextBlock;
      MessageBox.Show(nameBlock.Text + " " + nameBox.Text);
     }

当然,您需要将姓名添加到TextBoxTextBlock

<TextBlock x:Name="nameYourTextBlock Text="{Binding Path=Account.Name}" Grid.Column="0" Grid.Row="1" FontSize="28"/>

另外,我TextBox中没有看到任何XAML