Is it possible to set focus to a control inside the XamWebDialogWindow?
I've got a hidden XamWebDialogWindow with a TextBox in the Content template. When I set the Visibility property on the dialog to Visible I also want to set the focus to the TextBox control so the user doesn't have to click on it to enter text. I've tried calling the Focus method on both the dialog and the TextBox with no luck.
Thanks.
Hi Urfandurrani,
If you use a textbox inside a XamWebDialogWindow.ContentTemplate, it is possible to set the name of the TextBox:
="TestWin">
>
="testText"/>
it is better to use WindowState instead Visibility and when XamWebDialogWindow.WindowState=WindowState.Hidden Visibility will be Collapsed.
When change WindowState it is possible to attach to WindowStateChanged event.
Inside the event handler it is possible to add :
TextBox box = GetFrameworkElement(myWin, "myTB") as TextBox;
if (box != null)
{
box2.Focus();
}
To find element by name inside visual tree you can use a static method like that:
public static FrameworkElement GetFrameworkElement(FrameworkElement element, string ElementName)
if (element.Name.Equals(ElementName) == true) { return element; }
ContentControl control = element as ContentControl;
if (control != null)
object controlContent = control.Content;
FrameworkElement content = controlContent as FrameworkElement;
if (content != null)
FrameworkElement el = GetFrameworkElement(content, ElementName);
if (el != null) { return el; }
int numChildren = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < numChildren; i++)
FrameworkElement content = (FrameworkElement)VisualTreeHelper.GetChild(element, i);
return null;
I hope that this can help :-)
Kind regards!
Mihail