2010-10-11 74 views
1

我有類似這樣的列表視圖,並且想要訪問的文本框選擇項目在旁邊?WPF中ListView中的訪問控制

lsitview.blah.blah.findTheTextBox(「blah」)。Name =「good!」;

我怎麼能做到這一點?由於

<ListView Name="listview"> 
    <ListView.View> 
    <GridView> 
     <GridViewColumn> 
     <GridViewColumn.CellTemplate> 
      <DataTemplate> 
      <TextBox Text="{Binding}"/> 
      </DataTemplate> 
     </GridViewColumn.CellTemplate> 
     </GridViewColumn> 
    </GridView> 
    </ListView.View> 
</ListView> 
+1

什麼是你想達到什麼目的?答案取決於你想做什麼。 – 2010-10-11 02:42:52

+0

@ Ken Henderson我想從「TextBox」中讀取文本 – Towhid 2011-03-18 12:26:24

回答

2

沒關係,我figoure出來:

SequenceItem _item = dgSequence.SelectedItem as SequenceItem; 
      if (_item != null) 
       _item.IsReadOnly = false; 

      ListViewItem _lstItem = dgSequence.GetCurrentListItem(); 
      if (_lstItem != null) 
      { 
       TextBox _txt = _lstItem.GetDescendantByType<TextBox>(); 
       if (_txt != null) 
       { 
        _txt.GetBindingExpression(TextBox.IsReadOnlyProperty).UpdateTarget(); 
        _txt.Focus(); 
        _txt.SelectAll(); 
       } 
      } 

     public static T GetDescendantByType<T>(this Visual element) where T : Visual 
     { 

      if (element == null) return null; 

      if (element is T) return (T)element; 

      T foundElement = null; 

      if (element is FrameworkElement) 

       (element as FrameworkElement).ApplyTemplate(); 

      for (int i = 0; 

       i < VisualTreeHelper.GetChildrenCount(element); i++) 
      { 

       Visual visual = VisualTreeHelper.GetChild(element, i) as Visual; 

       foundElement = GetDescendantByType<T>(visual); 

       if (foundElement != null) 

        break; 

      } 

      return foundElement; 

     } 

     public static ListViewItem GetCurrentListItem(this ListView lstvw) 
     { 
      if (lstvw.SelectedIndex < 0) 
       return null; 
      ListViewItem _lstItem = lstvw.ItemContainerGenerator.ContainerFromIndex(lstvw.SelectedIndex) as ListViewItem; 
      return _lstItem; 
     }