Silverlight 4 – How to focus a TextBox that is contained in your Custom Control on Startup
Focusing a TextBox that’s inside a Custom Control isn’t so easy at startup of your application. Let me explain the problem that is also discussed on http://forums.silverlight.net/forums/t/151235.aspx. Imagine you’ve created a custom control that has a TextBox as Part-element. The Style that sets the Template would look like this:
<Style TargetType="local:SimpleControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SimpleControl">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBox x:Name="PART_Text" Margin="10"></TextBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You get the TextBox from the Template in the OnApplyTemplate-Method
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_partTextBox = this.GetTemplateChild("PART_Text") as TextBox;
}
Now imaginge you want to focus your SimpleControl in the constructor of your MainPage like below:
public MainPage()
{
InitializeComponent();
HtmlPage.Plugin.Focus();
yourControl.Focus();
}
The Problem that occurs is that the OnApplyTemplate-Method is called after the Focus-Method. So you don’t have the TextBox defined in your Template. The Solution is very easy. Just hide the Focus-Method from the base-class and set a flag. But keep in mind that this won’t work if you store your SimpleControl in a variable of type Control, cause then no polymorphic call is made to the Focus-Method defined in SimpleControl. Here a sample implementation:
public class SimpleControl : Control
{
private bool _applyFocusToPartTextBox;
private TextBox _partTextBox;
public SimpleControl()
{
this.DefaultStyleKey = typeof(SimpleControl);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_partTextBox = this.GetTemplateChild("PART_Text") as TextBox;
if (_partTextBox != null)
{
if (_applyFocusToPartTextBox)
_partTextBox.Focus();
}
_applyFocusToPartTextBox = false;
}
public new bool Focus()
{
if (_partTextBox == null)
{
_applyFocusToPartTextBox = true;
return true;
}
return base.Focus();
}
}
How to print a List<string> in Silverlight 4 Beta over multiple pages?!
public partial class MainPage : UserControl
{
private List _list;
private const double ROWHEIGHT = 20;
private const double PAGEMARGIN = 30;
public MainPage()
{
InitializeComponent();
_list = new List();
for (int i = 1; i < 101; i++)
{
_list.Add(i + " thanks to Thomas for this printing sample");
_list.Add("Visit http://www.thomasclaudiushuber.com");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_currentIndex = 0;
var pd = new PrintDocument();
pd.DocumentName = "AListFromThomas";
pd.PrintPage += pd_PrintPage;
pd.Print();
}
private int _currentIndex;
private double _currentTop;
private double _availableSpace;
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
var pageRoot = new Canvas();
e.PageVisual = pageRoot;
_currentTop = PAGEMARGIN;
_availableSpace = e.PrintableArea.Height - PAGEMARGIN*2;
while (_currentIndex < _list.Count)
{
var txt = new TextBlock { Text = _list[_currentIndex] };
if (ROWHEIGHT > _availableSpace)
{
e.HasMorePages = true;
break;
}
txt.SetValue(Canvas.TopProperty, _currentTop);
txt.SetValue(Canvas.LeftProperty, PAGEMARGIN);
pageRoot.Children.Add(txt);
_currentTop += ROWHEIGHT;
_availableSpace -= ROWHEIGHT;
_currentIndex++;
}
}
}
When the Button_Click-Eventhandler is executed, the List gets printed over 5 pages. You can easily print it to PDFCreator or XPS Printer to test it. The output looks like this:
Download the source here and enjoy. Give me feedback by entering a comment to this blogentry or via email on www.thomasclaudiushuber.com/blog.How to supress the Alt-Key in Silverlight’s TextBox
private void TextBox_TextChanged(object sender, TextCh… e)
{
if (_altWasPressed)
{
// remove the added character
var textBox = ((TextBox)sender);
var caretPos = textBox.SelectionStart;
var text = textBox.Text;
var textStart = text.Substring(0, caretPos - 1);
var textEnd = "";
if (caretPos < text.Length)
textEnd = text.Substring(caretPos, text.Length-caretPos);
textBox.Text = textStart + textEnd;
textBox.SelectionStart = caretPos - 1;
_altWasPressed = false;
}
}
private bool _altWasPressed;
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
_altWasPressed = e.Key == Key.Alt;
}
How to print dynamically created Images in Silverlight 4 Beta
WPF Printing: How to print a PageRange with WPF’s PrintDialog – that means the user can select specific pages and only these pages are printed
Silverlight 4 – the first Beta is here
Hey Thomas, what’s coming up next?
… what’s coming up next:
- WebTech-Conference – 16th November, Karlsruhe/Germany Another talk about datadriven Silverlight-Applications. Meet me at this conference for discussions about WPF, Silverlight, .NET in general, my books and other topics. Find more about the WebTech-conference on webtech
- Update of the WPF-book to .NET 4.0 and Visual Studio 2010 The WPF-book was written about .NET 3.5. Next year .NET 4.0 will be released. There are many new things introduced in WPF. The DataGrid- and DatePicker-Control, VisualStateManager, Animation Easing Functions, Layout Rounding and so on. I’m working on an update of the book that will be released next year shortly after the German Visual Studio Release.
- Writing a german book about Silverlight 4 Currently I’m working hard on my book about Silverlight 4. I've already written about 300 pages. The book will be released next year shortly after the Silverlight 4 release. There are no comments on the Silverlight 4 release, but at PDC in mid-November there’s a session about the Silverlight-Roadmap. Then we’ll know more. So stay tuned. Find more about the upcoming Silverlight-Book on the new silverlight-category on my homepage
… what I did the last months:
- Silverlight-Articles I’ve written six articles about Silverlight for the German dotnet-magazine. Download the articles beside others on www.thomasclaudiushuber.com/articles.php.
- PrioConference – 28th October, Munich/Germany I had a session about datadriven Silverlight-Applications. Find the Details on the new talks-category on my homepage.