Archive for the ‘WPF’ Category

Save / manage column layouts / sortings in WPF Data Grid

September 25, 2009

I present here a solution demonstrating Advanced Data Grid: a user control allowing you to save / retrieve / manage column layouts and sortings you have done in WPF Data Grid from WPFToolkit (see at Due to security rules of, it cannot be uploaded as zip file, please firstly remove the .doc extension of file  Advanced Data Grid for WPF. Solution is developed under VS 2008.

It is not Open Source, you are not allowed to use the source code.
Description of code see in: CodeBeispiel (in German!), using manual here: developer manual for Advanced Data Grid (in English).


Show custom Date format in auto-generated DataGrid column

June 25, 2009

The problem in WPF DataGrid is: data is always shown in US long format. Probably due to some bug in WPFToolkit. But we don’t want to change WPFToolkit. We try to avoid the issue.

Really useful link is:

The solution of Bryan Anderson:

<DataTemplate DataType="{x:Type DateTime}">
<TextBlock Text="{Binding StringFormat={0:d}}"  />

Doesn’t work by me ;-( ,

but the solution of FarrEver with AutoGeneratingColumn() event is a good idea.

But: it is still the US long! WPFToolikt doesn’t use computer’s short date format!

What we can do? We can hard code the format, but use the computer’s regional settings. Then your customers see the date format they used to see. Code:

Private Sub DGrid_AutoGeneratingColumn(ByVal sender As System.Object, ByVal e As Microsoft.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
    If e.PropertyType Is GetType(DateTime) Then
        Dim dataGridTextColumn As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
        If dataGridTextColumn IsNot Nothing Then
            Dim ShortDatePattern As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern
            dataGridTextColumn.Binding.StringFormat = “{0:” + ShortDatePattern + “}”

        End If
    End If
End Sub

Enforce DataGrid to regenerate columns

June 17, 2009

It is simple:

DataGrid.AutoGenerateColumns =False
DataGrid.AutoGenerateColumns =True
And columns will be regenerated!

Cancel Selection Change

May 20, 2009

The situation: the user has changed a selection in, say, combobox. Your event handler  Combobox_SelectionChanged() asks the user: “Are you sure?” and the user answers: “No! Cancel!” How can you cancel the selection?

One way is described in:

Really previous selection is e.RemovedItems(0). But if in event handler  Combobox_SelectionChanged()you assign:

Combobox.SelectedItem = e.RemovedItems(0)

then your event handler will be called again! And again.

Amazedsaint proposed to use global variable. Not very clean solution. I propose to remove event handler assignment before the command Combobox.SelectedItem = e.RemovedItems(0) and reassign it afterwards, e.g.:

Sub Combobox_SelectionChanged()

If MessageBox.Show(“Are you sure”,MessageBox.YesNo)<>MessageBox.Yes Then

Dim cbo as ComboBox=CType(sender,ComboBox)

RemoveHandler Combobox.SelectionChanged, AddressOf Combobox_SelectionChanged


AddHandler combobox.SelectionChanged, AddressOf combobox_SelectionChanged

End If

End Sub

One another issue with “Cancellation of User Changes” is changes made in binded data. If you don’t want to change data before the user assume it, you can use


and change your data directly or via BindingExpression.UpdateSource().

Hope it helps.