2016-04-22

WPF, XAML and MVVM: Group and Ungroup your records easily in your DataGrid

In this post I will show you how you can enhance your datagrid with grouping. It will be a long post, it is assumed you already know how to bind and display data.


Introduction

This is what we are going to achieve:

There are several features:
  • The groups are persistent even when the records change or the DataGrid is refreshed.
  • Grouping and Ungrouping is done using checkbox.
  • The expanders have their text formatted (the model store numbers not strings).
  • The expanders are indented, and the headers follow the indentation. 
In addition, I would precise that:
  • The records are not refreshed, only grouped and ungrouped (no additional query from database).
  • Everything is done using Attached Behaviors, Converters and Style, so it is completely reusable.
  • The MVVM pattern is not violated.

2016-04-20

WPF and XAML: How to display images on the buttons correctly.

You may have already noticed that when you place an image on button in WPF, it doesn't display it well. In this post I will show you how to make your buttons look nice.



On the left is the modified button, on the right is the original one.


Introduction

The best way to do this is to create a new style that you will apply to your buttons when there are holding an image.

Of course you can put as many properties as you want in your style, create more styles and reuse them as you need.

  • In my example the button initially has an opacity of 70% and when the mouse is over it, it becomes 100%. 
  • The button in the middle is disabled, in this case its opacity is 30%.
  • When the button is clicked, it becomes smaller and gives a visual feedback.


2016-04-19

WPF Behaviors:How to save your TextBox on Enter and Cancel it on Escape

Using WPF, if you are not yet familiar with the so-called Behavior you will discover how to you can create some and reuse them in all your designs.

Introduction

First of all, there are 2 types of Behavior: Attached Behavior and Blend Behavior. The main difference between them is that the Attached Behaviors are static and therefore limited in terms of usage, the Blend Behaviors are not static but require the blend SDK to be used. Blend Behaviors are available via the user interface like any other control using drag and drop, a good and recent description can be found here: 

Now let's have a look at how the Attached Behaviors can be used.

Let's say you have several TextBoxes on your wpf windows or usercontrol, where the user can type in text to be saved through Binding to some properties of your ViewModel.

In your XAML it looks like that:  <TextBox Text="{Binding Path=Dielectric}"/> 

And in your ViewModel there is your property:  public string Dielectric { get; set;} 

You have already certainly noticed the several Binding Mode options and know the difference:
 Mode=Default, OneWay, OneTime, OneWayToSource, TwoWay 

And you also know what means the Binding UpdateSourceTrigger property:
 UpdateSourceTrigger=Default, Explicit, LostFocus, PropertyChanged