Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <ListView x:Name="myList" ItemsSource="{Binding SomeList}">
- <ListView.View>
- <GridView>
- <GridViewColumn>
- <GridViewColumn.CellTemplate>
- <DataTemplate>
- <!-- Focus this! -->
- <TextBox x:Name="myBox"/>
- (myList.FindName("myBox") as TextBox).Focus();
- private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (myList.SelectedItem != null)
- {
- object o = myList.SelectedItem;
- ListViewItem lvi = (ListViewItem)myList.ItemContainerGenerator.ContainerFromItem(o);
- TextBox tb = FindByName("myBox", lvi) as TextBox;
- if (tb != null)
- tb.Dispatcher.BeginInvoke(new Func<bool>(tb.Focus));
- }
- }
- private FrameworkElement FindByName(string name, FrameworkElement root)
- {
- Stack<FrameworkElement> tree = new Stack<FrameworkElement>();
- tree.Push(root);
- while (tree.Count > 0)
- {
- FrameworkElement current = tree.Pop();
- if (current.Name == name)
- return current;
- int count = VisualTreeHelper.GetChildrenCount(current);
- for (int i = 0; i < count; ++i)
- {
- DependencyObject child = VisualTreeHelper.GetChild(current, i);
- if (child is FrameworkElement)
- tree.Push((FrameworkElement)child);
- }
- }
- return null;
- }
- var item = new SomeListItem();
- SomeList.Add(item);
- ListViewItem = SomeList.ItemContainerGenerator.ContainerFromItem(item); // returns null
- <DataTemplate x:Name="foo">
- <!-- Focus this! -->
- <TextBox x:Name="myBox"/>
- var tb = foo.FindName("myBox");
- Private Sub SelectAllText(ByVal cell As DataGridCell)
- If cell IsNot Nothing Then
- Dim txtBox As TextBox= GetVisualChild(Of TextBox)(cell)
- If txtBox IsNot Nothing Then
- txtBox.Focus()
- txtBox.SelectAll()
- End If
- End If
- End Sub
- Public Shared Function GetVisualChild(Of T As {Visual, New})(ByVal parent As Visual) As T
- Dim child As T = Nothing
- Dim numVisuals As Integer = VisualTreeHelper.GetChildrenCount(parent)
- For i As Integer = 0 To numVisuals - 1
- Dim v As Visual = TryCast(VisualTreeHelper.GetChild(parent, i), Visual)
- If v IsNot Nothing Then
- child = TryCast(v, T)
- If child Is Nothing Then
- child = GetVisualChild(Of T)(v)
- Else
- Exit For
- End If
- End If
- Next
- Return child
- End Function
- //textbox can be catched like this.
- var textBox = ((TextBox)sender);
- EmailValidation(textBox.Text);
- }
Add Comment
Please, Sign In to add comment