Skip to main content

Posts

Showing posts with the label WPF

DependencyObject in ViewModelBase - Good or Bad?

First of all, a very-very Happy New Year to all my readers. If you are reading this post, then I’m assuming that you have a working experience of WPF using MVVM.  Don’t worry, I’m not going to re-write a huge post on what MVVM is and where to use it? This blog is very small which talks about one of the best practices any developer implementing MVVM should follow. Well, let me provide you the context first. Last week I was going through one of my colleagues’ code and noticed few interesting things in his ViewModelBase class: It was holding few Dependency properties  It was inheriting  DependencyObject  How it sounds to you? Good or … ?  Of course, that is not at all a good practice as ViewModelBase is the base class for all the ViewModels and is not supposed to contain such code. Well, this is not the only reason.  There are many other reasons which are making this implementation a BIG NO.  1 st Reason – A DependencyObject was never meant to be a Source of a bi

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

Associating any file with an installed application

Recently I got a requirement to associate a file having custom extension with the installed version of our own application. So, whenever user clicks on that given extension file, it should open up directly in my WPF application along with the data population. So, let’s check out how you can achieve this: Step 1 - Creating an application: Create a WPF application or you can take any of your existing application. I’m working on WPF application (using MVVM) but the concept remains same for others, you can go with your Windows Forms or Console application also. That’s pretty acceptable. In my application, I'm taking few customer properties and a file path. Step 2 - Get key for Signing: For signing my assembly I’m using inbuilt feature of Visual Studio. Go to your project properties, navigate to Signing option as shown below:                                              If you already have a key then you can browse and associate it else you can always go ahead and

Handling UI control's events in ViewModel [Prism 5.0]

Recently I came across a requirement in which I was supposed to data bind a command from my viewModel to an event. In other words, my code-behind was not supposed to contain any code related to event handlers of a control. After digging more into Prism, luckily I found my answer. Above requirement can be achieved using InvokeCommandAction provided in Prism 5.0. Well, so my article will elaborate more on how to achieve this. InvokeCommandAction InvokeCommandAction consents us to invoke ICommand implementation on our viewModel to an event that exist on our control. It basically means, we are no longer bound or required to have a command property on a control in order to invoke a command. Now, you may say you have seen this similar thing before. And you are right because Blend SDK ships one. But the InvokeCommandAction which Prism provides is little bit different in two ways: First, it manages the state of the element. It updates the enable state of the control that is att

PopUps with Interactivity using ConfirmationRequest [Prism 5.0]

In continuation to my previous blog on PopUps with Interactivity , here we will see how to implement IConfirmation request. In order to make this post short, I'll implement IConfirmation request on top of previous example.  Let's open our viewModel and add property for InteractionRequest of type IConfirmation  as: public InteractionRequest<IConfirmation> ConfirmationRequest { get; set; } Now for every getter/setter we should have a corresponding command, which will help us in invoking this request: public ICommand ConfirmationCommand { get; set; } Just like NotificationRequest, we need to create instances of these objects in constructor as: If you will see above snippet closely, you will notice that in case of Confirmation, we are handling Status in slight different manner. Next step is to update our View as we did for NotificationRequest. Now everything is in place. Let's quickly run the application and click on PopUp button:

PopUps with Interactivity using NotificationRequest [Prism 5.0]

Just about every application has a need to notify user about an event or ask for confirmation before proceeding onto the next operation. Prior to MVVM, we would have used the MessageBox class in the code-behind. But for MVVM applications, that’s not the appropriate way as it breaks the separation of concerns from the view or viewmodel. There are lots of ways to show popups in MVVM application. In Prism, we just happen to use triggers. Triggers Triggers are used to initiate actions when a specific event is raised. So, it means we have to setup a view to detect the interaction request of event and then present an appropriate visual display for that request. What is required for raising events? Now for raising events, we need an event trigger. But not just any event trigger. We don’t want to use the built-in event trigger, instead Prism provide its own InteractionRequestTrigger. This trigger binds to the source object or the InteractionRequest object that exist in your viewm