Skip to main content

Posts

Showing posts with the label WPF

What's New In Prism 5.0?

Are you WPF, Silverlight or Windows Phone developer and used Microsoft’s patterns and practices library to build your applications? If you are, then you might want to know that Microsoft’s patterns and practices team have just released Prism 5.0. All the applications built using the previous versions of Prism are now broken. So, in this artifact, I’ll be discussing about the new assemblies, new objects and deprecated objects which can/can’t be used with Prism 4.1 and Prism 5.0. Downloading Prism 5.0 Prism 5.0 can be downloaded and installed either from patterns and practices site having URL as http://compositewpf.codeplex.com/ or by using Nuget package inside Visual Studio. Mentioned link also discusses about all the changes which are part of Prism 5.0 Supported Platforms Let’s have a quick look at the supported platforms of Prism 5.0. While working with previous versions of Prism (i.e. 4.1), one was able to create applications like WPF (.Net 4.0), Silverlight 5 and Window

Propagating Property Change for Static Properties

While working on any XAML based app, first thing which comes into mind is Binding . There are lot many articles on what is Binding and how it works. Don’t worry, I am not going to repeat all that stuff again. But definitely, I would like to touch upon few things which are base of my today’s write-up. To make any property bindable or let’s say to propagate property change, we usually follow one of the below two options: Implement INotifyPropertyChanged interface or Create an event with name PropertyNameChanged Point to notice here is, both the above options will work only on instance properties. Now what if my property is Static??? INotifyPropertyChanged is not going to work for static properties. None of the above options will make x:Static extension work. What to do now ? No worries, all these hazards can easily be overcome when you will jump to .Net 4.5. Approach 1: Property specific static event for each and every static property Let’s have a look a

Identify WPF version

If you want to figure out the current version of WPF installed on your machine, then one has to navigate to registry for below path: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup\Windows Presentation Foundation Version value on above path will give your the required information.

Cursor Position on Enter key

Recently, one of my followers requested for a code snippet to implement the ENTER key press. The requirement was to move the cursor on the next WPF UI element, whenever the ENTER key is pressed. Implementation of the above requirement is straight forward, if one has the knowledge of InputBindings and Command. Using these two, MoveFocus method of currently focused element, can be called, as shown in the mentioned below code snippet: Hope it will help you.

FallbackValue and TargetNullValue

One of the ability of binding in WPF is the ability to support Fallback values. Fallback values are used when a binding comes up with an un-appropriate value which in turn can't be set on binding target. There are 2 types of Fallback values:        TargetNullValue :     As its name communicate itself that when a source object’s property is null, then what is the alternate value you want to set for the target. So, whatever value is set for TargetNullValue, it will be set for target. Pretty simple, isn't it ? 2   FallbackValue :      This is used when a binding cannot come up with a value at all, based on the the data source and the path. Or in other words, FallbackValue is used when the property binded with source is not at all available. In that case, value supplied to FallbackValue will be considered at the target end. Getting into the code:  Now, let’s jump on to the code on how to use both of these. Here aim is to bind the given text box with EmployeeName

Gotcha with StringFormat

Hope most of you have used StringFormat property on your binding to render the formatted value on the user interface. But are you aware about one of its secret. Well before revealing that secret, let’s have a look at how StringFormat works with binding. Scenario problem: I am taking an example scenario, in which my text box will display amount till three decimals.  Now there are multiple ways to achieve this. One way can be by using Converters, another way can be by using StringFormat along with TextBox binding. Perhaps there can be more ways apart from these two ;) In below sample I am going to take StringFormat trick to achieve this and code to perform this operation is very straight forward as: <TextBox Text="{Binding Amount,StringFormat=f3}" /> With above code, whenever you will lost focus from your TextBox, given amount will be displayed as three decimal points. Till here everything is perfect as expected BUT with one downsize. Important

Converters in WPF

Converters give you a lot supremacy as it allows you to insert an object between a source and a target object. At high level, converters are a chunk of custom code that hooked up through the binding and data will flow via that converter. So, whenever data is flown from source to target, one can change the value or can change the type of object that needs to be set on target property. So, whenever data travels from source to target, it can be transformed in two ways: Data value: Here transformation will be done with just the value by keeping the data type intact. For example, for number fields, you can transform value to floating point number to an integer, by keeping the actual value as a float. Data type:  One can also transform the data type. For example, setting a style based on some Boolean flag, this is one of the most common example. Isn't it? Defining a converter: Defining any converter requires implementation of IValueConverter interface in a class. Thi