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,
it is true. Thank you for the notes.
Mihail
I've sorted this issue now without the need to use the WindowStateChanged event (which still does not get triggered if WindowState="Hidden" is included in the XAML code).
Apparently there's a known bug in Silverlight 3 (known by Microsoft presumably although not by me before today!) - calling the Focus method on a TextBox control (I haven't tested to see if it applies to other control types) does not work unless you call the UpdateLayout event first i.e.:
txtControl.UpdateLayout();txtControl.Focus();
See: http://www.c-sharpcorner.com/UploadFile/dpatra/SettingFocusofTextBoxInSilverlight310102009112352AM/SettingFocusofTextBoxInSilverlight3.aspx
Hi Mihail,
Thanks for that. Your sample does work but that appears to be because your dialog control is visible when your control loads. When I add:
WindowState
="Hidden"
to the <igDW:WebDialogWindow... declaration, run it and click the "Normal" button the WindowStateChanged event does not get triggered. I have to click the "Hidden" button and the "Normal" button again to get the code to work.
I've even tried setting the control's WindowState property to Hidden in code when the MainPage control has loaded but this doesn't work either.
Hi Urfandurrani,
You can use that sample:
<Grid x:Name="LayoutRoot" Background="Beige"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="40"/> </Grid.RowDefinitions> <igDW:XamWebDialogWindow x:Name="myWin" RestrictInContainer="True" WindowStateChanged="myWin_WindowStateChanged" 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> <StackPanel Grid.Row="1" HorizontalAlignment="Stretch" Orientation="Horizontal"> <Button Content="Normal" x:Name="btnNormal" Click="btnNormal_Click"/> <Button Content="Hidden" x:Name="btnHidden" Click="btnHidden_Click"/> </StackPanel> </Grid>
In C# code:
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; }
private void btnNormal_Click(object sender, RoutedEventArgs e) { this.myWin.WindowState = WindowState.Normal; }
private void btnHidden_Click(object sender, RoutedEventArgs e) { this.myWin.WindowState = WindowState.Hidden; }
private void myWin_WindowStateChanged(object sender, WindowStateChangedEventArgs e) { if(e.NewWindowState == WindowState.Normal) { TextBox box2 = GetFrameworkElement(myWin, "myTB") as TextBox; if (box2 != null) { box2.Focus(); } } }
That works on my sample good.
I hope yjat can help :)
gn
Thanks for the reply. Unfortunately this didn't resolve my problem as the WindowStateChange event does not fire when I change it from Hidden to Normal.
My dialog is defined as:
<igDW:XamWebDialogWindow Grid.Row="0" Grid.RowSpan="2" x:Name="dialogCustomerSearch" WindowState="Hidden" WindowStateChanged="dialogCustomerSearch_WindowStateChanged" HorizontalAlignment="Stretch" VerticalContentAlignment="Stretch" Header="Customer Search" Height="500" Width="500" IsModal="True" StartupPosition="Center" CloseButtonVisibility="Collapsed">
<igDW:XamWebDialogWindow.ModalBackgroundEffect>
<BlurEffect Radius="10" />
</igDW:XamWebDialogWindow.ModalBackgroundEffect>
<igDW:XamWebDialogWindow.Content>
<local:orderCustomer x:Name="ctrlCustomer" Margin="5" IsTabStop="True" CustomerSelected="ctrlCustomer_CustomerSelected" SelectionCancelled="ctrlCustomer_SelectionCancelled" />
</igDW:XamWebDialogWindow.Content>
</igDW:XamWebDialogWindow>
And in code, when a button is clicked I run this (in VB.NET - sorry :-) ):
dialogCustomerSearch.WindowState = Infragistics.Silverlight.WindowState.Normal
When I do this the WindowStateChanged event is not fired. It is fired however when I close the dialog!
Am I doing something wrong?