WPF 4 / VS2010: DataGrid IsReadOnly does not work with binding to boolean property - by Matt Jenkins

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


4
0
Sign in
to vote
ID 530280 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 2/2/2010 4:56:06 PM
Access Restriction Public

Description

Hello

I can't get a DataGrid column's IsReadOnly property working with a binding to a boolean property, but it works with "True"/"False".

The following works; the columns are readonly as expected:

<Window x:Class="InnCRMWPF4.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyApplication">
    <DataGrid ItemsSource="{Binding Source={x:Static local:Elements.Instance}, Path=ClientList, Mode=OneWay}">
        <DataGrid.Columns>
            <DataGridTemplateColumn IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <!-- ... -->
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn IsReadOnly="True"
                                Header="First Name"
                                Binding="{Binding FirstName, Mode=TwoWay}" />
        </DataGrid.Columns>
    </DataGrid>
</Window>


The following does not, even though the exact same binding syntax (to the 'Locked' property) works on other elements in the grid.

<Window x:Class="InnCRMWPF4.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyApplication">
    <DataGrid ItemsSource="{Binding Source={x:Static local:Elements.Instance}, Path=ClientList, Mode=OneWay}">
        <DataGrid.Columns>
            <DataGridTemplateColumn IsReadOnly="{Binding Locked}">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <!-- ... -->
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn IsReadOnly="{Binding Locked}"
                                Header="First Name"
                                Binding="{Binding FirstName, Mode=TwoWay}" />
        </DataGrid.Columns>
    </DataGrid>
</Window>
Sign in to post a comment.
Posted by Ratikanta on 8/30/2010 at 1:23 AM
Hi,
I faced the same Issue.The IsReadOnly Property Of WPF DataGrid Not Supporting binding.I finished my requirement in different way,means by putting one dataTemplate like given code,but i used IsEnabled Property of the controll.I think u can use in same way.

Regards,
Ratikanta Rout

<Custom:DataGridTemplateColumn Header="Refund" IsReadOnly="False">
                    <Custom:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox IsEnabled="{Binding ModeChange}" Text="{Binding RefundAmount,Mode=TwoWay}" Style="{DynamicResource txtDgChartSmall}" ></TextBox>
                        </DataTemplate>
                    </Custom:DataGridTemplateColumn.CellTemplate>
                </Custom:DataGridTemplateColumn>
            </Custom:DataGrid.Columns>
Posted by Arjan_Priva on 5/4/2010 at 3:06 AM
CanUserAddRows of The DataGrid also in silverlight 4.0 is not enabled.
Posted by Jackfather on 2/25/2010 at 1:07 AM
CanUserAddRows of The DataGrid of WPF In visual studio 2010 RC is not enabled.
Posted by Matt Jenkins on 2/4/2010 at 2:58 PM
Thank you very much for your feedback.
Posted by Microsoft on 2/4/2010 at 12:47 PM
The IsReadOnly property, like any property on a DataGridColumn, applies to the column as a whole. You can use it to mark the entire column as read-only, but you can't set some cells in the column as read-only and others as read-write (I suspect you want to do this as a function of the Locked property of each Client in the ClientList).

If you really want to use data binding to set the entire column as read-only, you'll have to use a binding with an explicit source ({Binding Source=...}). That's because the DataGridColumn is not part of the tree in the parent-child sense, and so it does not inherit DataContext from anywhere. It's in a property-value relationship with the DataGrid, via the DataGrid.Columns property, but DataContext does not inherit through such a relationship. The binding in your example tries to find somewhere to get its DataContext, but it fails and writes a message to your debugger's output window about "Cannot find governing FrameworkElement or FrameworkContentElement".
Posted by Microsoft on 2/3/2010 at 12:46 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)