WPF: Setting Focus in a Custom User Control

When you create a custom user control, it typically contains child user interface elements. To provide a proper “Focus” experience at run time, you need to do the following:


1. Select your user control in design mode and check the Focusable check box


2. Create a handler method for your user control’s GotFocus event. In the event handler method, add a line of code that sets focus to the child control you want to initially receive focus. For example:


private void AutoCompleteTextBox_GotFocus(object sender, RoutedEventArgs e)
{
  
this.txtTextBox.Focus();
}


Best Regards,
Kevin McNeish
INETA Speaker
Chief Software Architect, MM .NET Application Framework
www.oakleafsd.com

Silverlight: Fixing the "Tag Expander Does Not Exist" Compiler Error

If you create a new Silverlight Navigation project, then compile immediately with no changes, you will get the following compiler error:


The tag ‘Expander’ does not exist in XML namespace ‘clr-namespace:System.Windows.Controls;assenbly=System.Windows.Controls’


This is because the Expander element (as well as DockPanel, WrapPanel, HeaderedContentControl, Viewbox, and DataForm have been moved to the Silverlight Toolkit.


To fix this problem:


1. If you haven’t already done so, download and install the Silverlight Toolkit (http://www.codeplex.com/Silverlight)


2. Add the System.Windows.Controls.Toolkit assembly to your project


3. Add the following namespace reference to the top of your project’s ErrorWindow.xaml file:

xmlns:controlsToolkit=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit”4.


4. In the body of the ErrorWindow.xaml file, change the prefix of the Expander control from “controls” to “controlsToolkit”. Remember to do this on both the opening and closing tags:

<controlsToolkit:Expander Grid.Row=”2″ Header=”Details” Margin=”0, 10, 0, 0″ >

 

   <TextBox Text=”{Binding ErrorDetails}”
      
Height=”100″
      
TextWrapping=”Wrap” IsReadOnly=”True”
     
VerticalScrollBarVisibility=”Auto” />

 

</controlsToolkit:Expander>

Best Regards,
Kevin McNeish
INETA Speaker
Chieft Architect, MM .NET Application Framework
www.oakleafsd.com