Here I will post problems I and my colleagues met and solutions we found.

Wednesday, December 24, 2008

Sorting in WPF DataGrid

As you could see from my previous post, I am using WPF Toolkit DataGrid now. One of the reason to do it is to sort data. Sure, it is possible to do sorting in ListView, but using DataGrid I can do it with less efforts.

The only problem was that what I had as data source was regular BindingList, that does not support sorting. So, sorting just didn't work.

The solution I found the easiest is using Linq. Since I assigned my data source in code, not XAML, there was no inconvenience at all.

Instead of line


ItemsSource = list


I used


ItemsSource = from item in list select item;

Thursday, December 11, 2008

Changing background color for header in WPF Toolkit DataGrid

Today I decided to look at DataGrid from WPF Toolkit, available at CodePlex. As you may expect, the first thing I needed to do is to change it look. I started from changing background and immediately stuck.

1. Changed Background - didn't work for everything
2. Changed RowBackground - still not everything.
3. And then I added style for DataGridColumnHeader - now I had geader, but still some small rectungles remained gray.
4. And then something strange happened, I added style for DataGridRowHeader with yellow background, but result was completely unexpected. I got what I wanted, but I still don't understand how.

Here is my result.


<dg:DataGrid x:Name="grid" Background="Red" RowBackground="Red" IsReadOnly="True" GridLinesVisibility="None" >
<dg:DataGrid.Resources>
<Style TargetType="{x:Type dgp:DataGridColumnHeader}">
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="{x:Type dgp:DataGridRowHeader}">
<Setter Property="Background" Value="Green"/>
</Style>
</dg:DataGrid.Resources>
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="Test column"/>
</dg:DataGrid.Columns>
</dg:DataGrid>