Skip to main content

Posts

Showing posts with the label xaml

AccessKey not working on WPF ContentPresenter

Recently I received a query from one of my friends stating that access key is not working in his WPF project when he is using ContentPresenter. So, I thought to share a post on it as it may be helpful for other reader also. Before digging directly into the problem, first let’s see what happens when access key is set directly on the Content property of a WPF Button. Below is my code for setting a Content on a Button: < Grid >         < Button Height ="39" Width ="100" Content ="_Save"/> </ Grid > If you will run your application with above snippet, you will notice that there is no underscore coming in front of word Save. But as soon as you press ALT key, underscore comes up. Which is an expected behavior :) Now tweak the code a bit and instead of setting content directly on a button, do it on ContentPresenter as shown below: < Grid >         < Button Width ="95" Height ="34" B

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

UpdateSourceTrigger in WPF

This is a property on a binding that controls the data flow from target to the source and used in two-way databinding scenarios. The default mode is when focus changes but there are number of other options available, which we will see in this article. Data trigger scenarios: Let’s talk about some of the data trigger scenarios.  By default, modified values in the binding controls only get pushed down when we do activity related to focus change like tab out, minimizing and maximizing window, etc. ·     In some scenarios we want to update values as quickly as possible, or let’s say with every key stoke.  So, to achieve such kind of scenarios Microsoft provided  UpdateSourceTrigger .  Properties available with  UpdateSourceTrigger : Default – This is the default value and it means a lost focus for most of the controls LostFocus – Value updation will be on hold until the focus moves out of control PropertyChanged – Value updation will happen whenever a target property