Showing posts with label DependencyProperty. Show all posts
Showing posts with label DependencyProperty. Show all posts

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-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