Friday, February 12, 2010

ElementBinding,Binding Modes & UpdateSourceTrigger Property in Silverlight

Silverlight has a great binding syntax and its two way binding approach is a life saver, also it gives room to various new and creative concepts which developers can use to create really cool apps and features.

In this article i will talk a little about the Two Way Silverlight binding and the UpdateSourceTrigger Property.

Consider an example where we have a TextBox and a Silder control, now we want our TextBox to change its numeric text as we move the thumb of the slider control, now doing this is very simple in Silverlight by the element to element binding syntax, you just have to set the textbox text property to bind to the element slider and set its binding path to the value property of the slider control.

<Slider x:Name="slider" Maximum = "40" Value= "10" />
<TextBox x:Name="tbSliderValues" Text="{Binding ElementName = slider,Path = Value}"/>

So by the above code what you can do is as soon as you change the slider value i.e as you move the slider thumb the text in the textbox gets changed to the current value of the slider.

Now what if you change the text in the textbox , will the same thing works in the reverse order as well , meaning changing the text in the textbox will change the slider value , is it possible ????

Yes this is possible with a very simple tweak , just say thanks to the TwoWay DataBinding syntax of Silverlight

Just modify your code as given below and it will work like charm.

<Slider x:Name="slider" Maximum = "40" Value= "10" />
<TextBox x:Name="tbSliderValues" Text="{Binding ElementName = slider,Path = Value,Mode = TwoWay}"/>

Also to make a little point here about the different types of mode available in Silverlight, basically there are 3 types of mode in Silverlight :-

  1. OneWay
  2. TwoWay
  3. OneTime

One way is used when you know that the changes have to take place in only one direction meaning you can use oneway mode with Slider and TextBlock control in a simple case.

TwoWay mode is used when you want both the binding elements to reflect changes when either one is changed.TwoWay binding mode is the most commonly used mode in silverlight.

OneTime mode is used when you already know that you need to perform binding only one time and later the values related to that thing will never change in your application or if ever they are changing you are not reflecting those changes to your UI.

That said you have got the idea about the binding in Silverlight,now consider a situation in which you have a textbox and on the textchanged property you want to perform the binding to the slider, by setting the mode to two way will only work in this case when you loose focus from the text box i mean it will by default not work on the textchanged event.

So here what we have to do is explicitly force an Update to the property value.

private void tbSliderValue_TextChanged(object sender, TextChangedEventArgs e)
{
BindingExpression expression =
tbSliderValue.GetBindingExpression(TextBox.TextProperty);
expression.UpdateSource();
}

Now this will update your property everytime the textchanged event is fired for the textbox.

Now if you know that you will not need to update this using the default behaviour as in the case above you can reduce overhead by specifying the UpdateSourceTrigger = Explicit which tells that you already have set the update behavior explicitly so no need to check for the updates

<Slider x:Name="slider" Value="1" Maximum="100" Minimum="1" Width="300" Height="20" />
<TextBox x:Name="tbxSliderValue" TextChanged="tbxSliderValue_TextChanged" Text="{Binding Value, ElementName=slider, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="300"/>

UpdateSourceTrigger property has only two values one is Explicit and another is Default

Happy Programminig!!!!!!!!!!!!!

Submit this story to DotNetKicks

del.icio.usSave Total0 users

Monday, January 18, 2010

Cannot get the list of output files from the project

If you are a Silverlight developer who likes to keep himself updated on the latest stuff around in technology then you might face this problem. Actually this happens when you upgrade your system to VS2010 , by update i don’t mean that you are changing the target framework to be used to .net framework 4.0 update here just means that your .csproj file now knows that it has to open itself with VS2010 but after sometime if you feel that VS2010 beta is unstable, which looked to me also then you want to revert back to old and tested VS2008 everything can be reverted back without problems but when you will build the project in VS2008 you will notice that it complains “Cannot get the list of output files from the project” this is because the ToolsVersion used is still 4.0

The simple solution is to just go to the .csproj file in any of the text editors and modify the ToolsVersion in every project in the solution which contains your silverlight project.

Just replace the ToolsVersion = 4.0 with ToolsVersion= 3.5 and your solution will start working again.

That’s it.

Happy Programming!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Submit this story to DotNetKicks

del.icio.usSave Total0 users

Sunday, January 17, 2010

Wireless network problem in WS2008

If you are using WS2008 with your wireless network card or you are using WS2008 on a laptop, you might face a problem that after installing properly the driver of the wireless device WS2008 still couldn’t find any wireless networks.

This is not a bug but a simple issue i.e by default WS2008 doesn’t install the wireless LAN service so you have to install it manually to make your wireless card running.

At first i also got a bit frustrated as to why my wireless network is not working but soon i found out this solution, it was not so direct so i thought to share the solution with you all so that others having the same problem might feel easy to configure it.

Just go to the features section in the server manager and click on “Add Feature” then select “Wireless LAN Service”

After installing the wireless LAN Service your wireless device would start finding the networks.

 

Happy Programming !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Submit this story to DotNetKicks

del.icio.usSave Total0 users

Friday, January 15, 2010

HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

If you are using IIS 7 then you probably might face this error.It’s a little frustrating when such errors occur and simple IIS 6 or IIS 5.1 users when it was really simple to configure and run a website, those users feel really hooked up.

I myself faced a lot of issues but as you progress with IIS 7 you will find it simple to configure and very easy to manage.

As far as this issue is concerned in the earlier versions of IIS we used to just set the directory browsing checkbox to be checked in order to enable directory browsing on the virtual directory or the website, its really simple in IIS 7 also just click the directory browsing option in the configuration section and on the right side of the directory browsing view you will find enable by default directory browsing is disabled.It will be more clear from the below image.

This will solve your Directory Browsing issue for IIS 7.Stay tuned for more on IIS 7 tips and tricks series.

Happy Programming !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Submit this story to DotNetKicks

del.icio.usSave Total0 users

Monday, December 14, 2009

Error 1 The “Validatexaml” task failed unexpectedly.System.IO.FileLoadException.Could not load file or assembly.

If you are facing the ValidateXaml exception and your build fails then it might be due to the new feature of blocking the downloaded content.

This means whenever you download a project from the internet it is quite likely that windows will automatically block its content for few dll’s and if it’s so you will get this error.

So the easy solution is to look for the file for which its complaining as in the above solution its the Activity control for which it’s complaining, so just navigate to that file right click on the file and press unblock and that’s it , go  and rebuild your solution and everything will work as desired.

I have myself faced this problem in Windows Server 2008 and Windows 7.

You might try this also in Windows Vista as well.Hope this helps.

Happy programming !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Submit this story to DotNetKicks

del.icio.usSave Total0 users