Skip to main content

Posts

Showing posts with the label Converter

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

Troubleshooting data binding

We all know that DataBinding is one of the most powerful concept of WPF. So, today I thought to write something on how to troubleshoot data binding related issues while working with any XAML based application. Here I'll not talk about what and how data binding works, instead I'll jump directly on the relevant part. So, let's start by picking up the troubleshooting methods which can make developer's work bit easy. Way 1: Using Visual Studio output window Visual Studio provides high level information about binding which is sufficient to resolve very small problems like name mismatch, etc. Let's understand this along with a code snippet: <Grid> <TextBlock Text= " {Binding ElementName=label, Path=Hello, Mode=OneWay}" /> <Label Content= " Welcome" Name= " label" /> </Grid> Now open your output window and press F5, application will launch. In output window, you will notice that the message