WPF Grid and Read Only Columns

In an attempt to provide a total summary column in a WPF data grid, I attempted to bind a read only column of a DataTable to a DataGridTextColumn of the WPFToolkit data grid.

Initially, I tried to do the way I’ve been doing every other column.

<wpfToolkit:DataGridTextColumn
     Header="Total"
     DataFieldBinding="{Binding Total}"  />

Unfortunately that gives us the following error:

A TwoWay or OneWayToSource binding cannot work on the read-only property ‘Total’ of type ‘System.Data.DataRowView’.

OK, that makes sense, we should just adjust the mode of the binding and try again.

<wpfToolkit:DataGridTextColumn
     Header="Total"
     DataFieldBinding="{Binding Total,Mode=OneWay}"  />

….but we get the same error. After a few quick Google searches, I found someone else that already pointed out the issue and a possible workaround.

ReadOnly columns is missing from the CTP, but that is not a huge problem, you can easily accomplish ReadOnly behavior by using DataGridTemplateColumns and replacing the templates with read-only controls ( like TextBlocks).

A quick change of the column to a DataGridTemplateColumn and it looks like we are off to the races.

<wpfToolkit:DataGridTemplateColumn Header="Brand">
    <wpfToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Brand,Mode=OneWay}" />
        </DataTemplate>
    </wpfToolkit:DataGridTemplateColumn.CellTemplate>
</wpfToolkit:DataGridTemplateColumn>

NOTE: After another bit of searching on the web, the WPF Toolkit – October 2008 was released last week. This negates the need for the workaround for read only columns.

<wpfToolkit:DataGridTextColumn
     Header="Total"
     Binding="{Binding Total}"
     IsReadOnly="True" />

3 Comments so far

  1. Jonathan on December 31st, 2008

    Huh, cause I just downloaded the latest Toolkit and am still having the issue. Pretty annoying.

  2. Brian on March 19th, 2009

    Jonathan, this does work here is a complete example:

    I think you need the IsReadOnly and the Mode=OneWay mentioned…

    In the mean time I have another bug, trying to get the “Header” to databind!

  3. Brian on March 19th, 2009

    Sorry, forgot about the < and > formatting in the example.
    Heres the example again:

    <MSGrid:DataGridTextColumn Header=”Channel” Binding=”{Binding Path=Channel, Mode=OneWay}” IsReadOnly=”True”/>

Leave a Reply