Skip to main content

Posts

Showing posts with the label xaml

Binding Source Objects in WPF

Recently I received a request from one of the followers on ways to bind source objects. So, I thought it would be a nice topic for a post. Hence, it's here. For a binding to work, there should be a source object. The selection of source object is totally dependent on the way binding is used. If no binding source is provided, then bydefault DataContext of an element is used. The most common way is to set DataContext on parent element and let it flow to all its children. But apart from this, there are 3 other ways by which one can point to source object:  1) RelativeSource - Relative source is one of the property on a binding that can point to relative source markup extension which tells where the source can be found in hierarchy. In simple words, it is the relative path in the element hierarchy. 2) ElementName - Another way of specifying source is by using ElementName in which other element present in the current UI can be used as a source object. Here the source object

Dynamically selecting DataTemplate for WPF ListView - Way 1

Recently I get a chance to work on a project in which requirement was to select different different types of data templates based on the data. I faced bit difficulty, but afterwards I was manage to get it work. Let's have a look at the scenario first: Inside a WPF ListView, there are two GridViewColumns for storing Name and Age. Now requirement is to highlight the name of the person based on certain age criteria. If age is below 25, then name should be highlighted as red else it should be in green color, with desired background. I know there are ways to implement this using the concept of triggers, but I personally refrain from using triggers due to performance fall back. So, what else ??? The option left was dynamically selecting data templates. In this post I am going to write about the simplest approach and later on I'll do the same thing using Dictionary. Well, let's begin with our code. First of all, I created a class named Employee and added some data which w

Better way to play sound file on WPF button click

Recently I was working on a XAML based application, in which my requirement was to play a sound (.wav file) whenever a given button is clicked. So, to achieve this, I wrote a below snippet: My above snippet worked but at the same time, I feel a noticeable delay in beep sound and that make me analyze further and write a blog post :) Now question is, why there is delay between button click and sound ??? Well, there is a simple concept behind it, which I missed while implementing above requirement :( Reason is, the event hierarchy.  Most of us might be aware that Click is a bubbling event, which means event will be fired from the control who initiated it. So, in our case, whenever button is clicked, it bubbles from button to window. And that's the reason, click event handler is executing before the window event is triggered, which is ultimately leading to delay. Now, how to handle this??? Method 1: Then I thought to write a preview event, which will be fired

DataTemplating Overview

DataTemplate is a very powerful concept which allows you to provide a visualization for your objects in your application. DataTemplate objects are very useful specially dealing with collections. If you bind your collection with any of the ItemsControl, say ListBox then by using a DateTemplate one can change the appearance of data objects very easily. Well, now let's create a DataTemplate quickly. Let's start by creating a class called Employee: FirstName and Age will be our business objects that will reside in our application. Now we will go ahead and show the value of these objects on screen in a WPF application. For that our XAML will look like: Now looking at the code-behind: In code-behind, we have set the values for FirstName and Age with DataContext of main window. So, that XAML can bind to these values via EmployeeDetail property. At this point of time, if you will run the application, you will see: By above image yo

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