Skip to main content

Posts

Showing posts with the label WPF

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

Validating a WPF textbox to accept only negative decimal numbers

Recently working on one of the project, I came across an requirement of validating a TextBox in which Textbox should allow numbers only between range -999 and 0. If you will surf on internet, you will perhaps find many solutions to this problem in which developers create their own versions of TextBox either by creating a Custom/User controls or by inheriting it. But problem of this approach is - you would need to replace your TextBox definitions with your newly created TextBox. Sometimes this solution can be opted but sometimes it is not feasible to do so. This article describes on how to enhance existing WPF TextBox to make it happen. So, the approach I am proposing here doesn't require the replacement of existing TextBox control. Whilst it uses the very basic concept of Events. Well, enough of theory. Let's move on to coding part: XAML code: Code-Behind code: Hope you like this clean approach.

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

Formatting strings in binding

Recently I get a chance for a code walk through of an WPF application in order to achieve better performance. So, during my review process, I came across this below code snippet:   <WrapPanel>         <TextBlock Text="{Binding Path=FirstName,Mode=OneWay}"/>         <TextBlock Text=" "/>         <TextBlock Text="{Binding Path=LastName,Mode=OneWay}"/>     </WrapPanel> If you will closely analyze this given snippet, you will definitely get a way to optimize it. Well, I am talking  about formatting the string as well as binding part. As most of you are aware that we have a property named StringFormat since .Net 3.5 SP1. So, why can't we use this StringFormat for our binding too. If you want to change the above code to incorporate StringFormat , then it will something look like: <WrapPanel>         <TextBlock>                 <TextBlock.Text>                     <MultiBinding String

Resizing TextBlock with resizing of window

When dealing with XAML, many of you might have came across resizing issues in which your control is getting resize but the text/image inside the control is not getting resize as per the window size(it can be other user control). So, to deal with this Microsoft provide us with a Viewbox control, in which user can set the resizing aspects as per the need and requirement. So, this article focuses mainly on the properties provided by Viewbox control with proper sample code and output. MORE...

Safest way to use RaisePropertyChanged method - MVVM series 3 of n

Background In my previous post on  Simplest MVVM Ever , I gave the simplest overview of implementing MVVM. Now moving forward, I thought to extend my previous post by picking individual areas. And this time, I selected  RaisePropertyChanged . All the developers working on XAML related apps are very well aware about the use of this RaisePropertyChanged. I am sure, most of us are also aware on where to use this. But does everyone aware about what is the proper way or let's say generic way to use it ??? Well, even if you are not aware, no problem. At the end of this post, you will surely take home a useful tip on using  RaisePropertyChanged . Introduction As we know that MVVM provides a loose coupling methodology. At lot many places it is very useful to get benefit of such architecture, but at the cost of your alertness. Because when we are talking about MVVM, we usually says that ViewModel has no knowledge about View and properties are the way, who binds View and ViewModel to

Simplest MVVM Ever - MVVM series 2 of n

Continuation to my previous article on  WHY and WHEN of MVVM , this time, I am going to tell something on how we can create a simple MVVM application with minimal complexities. I am writing this article for those, who just want to see quickly how MVVM works. It doesn't do anything fancy and uses some basic databindings and commanding stuff. This article will be helpful for all those who are looking for a quick example of how to hook the View to the ViewModel and how commands plays a role.  Today if you will surf internet, you will came across 1000s of articles discussing on what is MVVM and what are the major building blocks of it. So, I am going to repeat the same crazy thing again. But definitely, I'll brief you about major building blocks in layman, in order to make it easier to understand for beginners. Excited, eh ???  Layman introduction of building blocks    So, let's quickly start with three main layers of MVVM. Model  - Model contains the classes which ar