Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
415
Setting focus to a control in an XamWebDialogWindow
posted

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.

  • 4666
    Suggested Answer
    posted

    Hi Urfandurrani,

    If you use a textbox inside a XamWebDialogWindow.ContentTemplate,
    it is possible to set the name of the TextBox:

     

     

     

     

     

     

     

     

     

     

    <igDW:XamWebDialogWindow x:Name="myWin" RestrictInContainer="True" LayoutUpdated="myWin_LayoutUpdated" Width="200" Height="200" Header

    ="TestWin">

     

     

    <igDW:XamWebDialogWindow.ContentTemplate

    >

     

     

    <DataTemplate

    >

     

     

    <Grid

    >

     

     

    <TextBox x:Name="myTB" Text

    ="testText"/>

     

     

    </Grid

    >

     

     

    </DataTemplate

    >

     

     

    </igDW:XamWebDialogWindow.ContentTemplate

    >

     

     

    </igDW:XamWebDialogWindow>

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

    FrameworkElement el = GetFrameworkElement(content, ElementName);

    if (el != null) { return el; }

    }

    return null;

    }

    I hope that this can help :-)

    Kind regards!

    Mihail