2012-07-10 57 views
0

你好,我需要从列表框选择的项目获取值流汗文本框的值。请注意,数据模板处于数据绑定状态。这里是XAML:从数据绑定的ListBox

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

我需要选择文本框的值改为event.I试图像这样...

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

但它没有工作!

回答

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

假设你有类(MyClass的)对象列表,您已databinded到列表框中

处理程序gesturelistener水龙头到的DataTemplate

添加在处理程序中做到这一点:

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

     } 

你有当前所选项目的对象,你可以访问所有的类变量。如(开始时间等)

+1

这是列表框的数据绑定 ** AppointmentResultsData.DataContext = e.Results.Where(S => s.Subject = X!); ** 这里e是** ** AppointmentsSearchEventArgs – Mushfiq 2012-07-10 11:36:36

+1

没有得到你,请分享一些代码 – CognitiveDesire 2012-07-10 11:54:37

+1

列表框绑定的数据是这样的: \t 无效Appointments_SearchCompleted(对象发件人,AppointmentsSearchEventArgs E) { \t \t \t AppointmentResultsData.DataContext = e.Results.Where(S => s.Subject!= x); } – Mushfiq 2012-07-11 04:33:41

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

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

+1

我已经命名了文本块,但仍然无法访问!给** NullReferenceException异常! – Mushfiq 2012-07-11 04:28:12

+0

** var listBoxItem = AppointmentResultsData.SelectedItem作为ListBoxItem; ** 给出所选项目null,但是 ** var listBoxItem = AppointmentResultsData.ItemContainerGenerator.ContainerFromIndex(AppointmentResultsData.SelectedIndex)作为ListBoxItem; 的TextBlock namebox中= listBoxItem.FindName( “txtAccount”)作为TextBlock的; MessageBox.Show(nameBox.Text); ** 给我所选择的item.Then的MessageBox给出** **的NullReferenceException – Mushfiq 2012-07-11 04:49:55