tags:

views:

18

answers:

1

Hi,

I have a listview like this and would like to access the textbox of SELECTED ITEM in side?

lsitview.blah.blah.findTheTextBox("blah").Name="good!";

How am I able to do that? Thanks

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

Never mind, I figoure it out:

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