This is just another small but helpful tip for Windows Phone 7 developers. Imagine that you want your user to type in some input, that you need to proceed. One can handle the precessing by offering a “confirm”-Button or something like it or you can just react on the Enter-Key of the softkeyboard.which is presented, when a user clicks in a textbox.

So, how is this accomplished? The Textbox-Control does not provide a OnEnter-event, all one can be found is something called KeyDown-event. This event is what we will use for our need.

In the XAML of your appropriate TextBox you can add a method to the KeyDown which will be fired if a key enters the TextBoxControl.

I called mine OnEnter and you can easily check, which key of the softkeyboard was “entered” 🙂

It`s that simple 🙂

This blogpost is more like a little hint or tip, if you want to develop your application with the MVVM – design pattern. Within Windows Phone 7 apps you can easily navigate through PhoneApplicationPages in the code-behind of your views with the NavigationService – Class.

But what if you handle some logic/calculation within a ViewModel and you want to navigate to another View on the basis of the result, you cannot access the NavigationService in this static context.

The solution for this is very simple! All you have to do is the following:

  • In the appropiate ViewModel, just create a property of type NavigationService, like so



  • using the NavService like you do in code-behind

  • and don`t forget to set the Property in the code-behind of the approriate view, so that the NavService is valid and to avoid a NullReferenceException 🙂 You can do this in the constructor or any other initialization method.

I hope, that this little tip is useful for someone.

As the title suggests, this blog post is all about bulding a UserControl for Silverlight, Windows Phone 7 or WPF development projects. I want to show this with a really simple example, inspiried by Silverlight in Action 4 by Pete Brown!

Open up Visual Studio and choose the class library project template for your target (e.g. SL or WP7)

In the solution explorer, rightclick on your project and add a new item –> <silverlight or WP7,…> user control! Now you see an almost empty xaml-file of your control. For this example we simple build a lockable textbox, so delete the the default grid and add a StackPanel and put a TextBox and a CheckBox in it.

Like in the picture, give your Text- and CheckBox a name. Cause we`re building a “lockable” Textbox, we want to set the IsReadOnly-Property of the TextBox to TRUE, if the user checks the CheckBox. This for example can be accomplished by DependencyProperties and binding in the code-behind or by writing  a simple converter-class, which we will be doing!

Now add a new class to your project and name it IsLockedConverter.cs or any other name you wish :). Cause it`s a converter we have to inherit from the IValueConverter interface, which provides us two methods, Convert() and ConvertBack(). We also have to inherit from the TextBox class, because we want to have access to the IsReadOnly-property of our TextBox.

In the Convert() method we have to return wether the given value is true or false. Just have a look at the implementation.

After this, we only have to add our new converter to our UserControl and using it! The first step is to add the appropriate xmlns in the header of the UserControl, in this case its something like this:

xmlns:converters=”clr-namespace:CustomControls.Converters”

Maybe your namespace is slighly different than mine. The second step is to add our converter to the UserControl.Resources specified by x:Key for “calling” it later on.

<UserControl.Resources>

<converters:IsLockedConverter x:Key=”IsLockedConverter” />

</UserControl.Resources>

You can name your converter whatever you like. So now the interesting part of using our converter. Go to your TextBox declaration in the xaml and add the IsReadOnly-Property and add the following binding:

…IsReadOnly=”{Binding ElementName=myCheckBox, Path=IsChecked, Converter={StaticResource IsLockedConverter}}”

With this line of code you bind the CheckBox to the IsReadOnly-Property of your TextBox. ElementName is the specified name of your Checkbox and the Path is the CheckBoxs property. After the Path you see the “Converter=” there you have to add our converter as a static resource. So whenever the user checks the checkbox this boolean value is passed to our converter and sets the IsReadOnly property of the textbox.

That`s easy isn`t it?! I hope that this is useful for the one or the other.

—UPDATE

You can grab the code here on GitHub…  https://github.com/cordellcp3/Various/tree/master/CustomControls/

Silverlight Windowless-Mode?

3. November 2010

When you want to embed HTML or something like that in a IFrame wihtin a Silverlight application and you come across an error which says that you have to change the windowless parameter to true  and one don`t know where to do this. Just open up your …TestPage.aspx or …TestPage.html file, which hosts your Silverlight  application.

Scroll down until you find <div id=”silverlightControlHost”> and simply add the windowless parameter like so:

Est voila, that`s all one got to do…