Tuesday, May 21, 2013

Quick challenge #2: how to truncate a string in C# and keep word boundaries (do not split a word in two)

[Q] How to truncate a string in C# with more than 10 characters and keep word boundaries (do not split a word in two)?

[Examples] "Good morning test." -> "Good morni..." (wrong), "Good morning..." (correct)

Thursday, May 16, 2013

How to make a consistent bind in WPF.

Hello everyone, to make your first bind can be a very traumatizing experience and even for someone with a little more experience a simple mistake can drive you crazy. So in this post we are showing some tips and good practice to make a consistent binding. First of all we recommend the Josh Smith post, that in fact is one of our references for this little post.

Friday, May 10, 2013

Wednesday, May 8, 2013

How to Bind to a DataGrid when its columns are not known at compile time (ItemsSource is bidimensional)

Making a Bind in a WPF DataGrid is not the the most difficult task. In fact, there are dozens of posts explaining how to do it (http://wpftutorial.net/DataGrid.html, http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples). However, they all have one detail in common: they assume that the columns of the DataGrid are known at compile time, ie., each column is a reference to a property of class that represents a row in the DataGrid.

An example: the class Customer has the properties Id, Name and Age. The DataGrid displays a list of customers. Therefore, it is necessary to create a column for each property. To do so, simply create in xaml, three instances of the class DataGridTextColumn and make a bind to each of the 3 properties, like following code:

How insert a button in a datagrid row header

Sometimes we need to do some strange things, like adding a button in a row header of a datagrid. In my case I am looking for something like a treeview datagrid, and adding a button in a datagrid row header is one of the things we need to do (here the part 1 of my saga, where we show how to collapse lines in a datagrid). So, in this post we are going to show some tricks to let you bind your View Model Property with your button properties.