WPF Datagrid Text column allows text enter even when row disabled

Jul 16, 2010 at 4:42 PM
Edited Jul 16, 2010 at 4:45 PM
Hi, I have a datagrid displaying a set of records with one of the columns being a DataGridTextColumn. The following is the text column along with the style assigned to it. <dg:DataGridTextColumn Header="Value" Width="*" HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}" CellStyle="{StaticResource ValueCellStyle}"> <dg:DataGridTextColumn.EditingElementStyle> <Style TargetType="TextBox"> <Setter Property="Foreground" Value="Blue"/> </Style> </dg:DataGridTextColumn.EditingElementStyle> <dg:DataGridTextColumn.Binding> <Binding Path="MasterValue"/> </dg:DataGridTextColumn.Binding> </dg:DataGridTextColumn> <Style x:Key="ValueCellStyle" TargetType="{x:Type dg:DataGridCell}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type dg:DataGridCell}"> <Grid> <Grid Background="{TemplateBinding Background}" > <ContentPresenter HorizontalAlignment="Right" VerticalAlignment="Center"/> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="IsTabStop" Value="{Binding Path=IsEnabled}"></Setter> <Setter Property="IsEnabled" Value="{Binding Path=IsEnabled}"></Setter> <EventSetter Event="PreviewKeyDown" Handler="DisabledTextBoxKeyHandler_PreviewKeyDown"/> <Style.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="Gray"/> </Trigger> </Style.Triggers> </Style Some of the rows have the text column disabled based off the property IsEnabled on the itemsSource object. This basically prevents me from clicking on the row and changing its value when its disabled. Tthis works fine however when i am in a row which is enabled and change its value and hit 'enter', supposing that the following row is disabled it still allows me to type in '1' character even in a disabled cell. I thought that i could handle this problem by adding a previewKeyDown handler and handle the event if the cell is disabled but the problem is that this handler is not called when the cell is disabled and only called in case its enabled. I cant seem to figure out why the datagrid allows me to type in '1' character for a disabled text column.. Any help on this would be great . (I posted this on msdn and someone mentioned that it could be an issue with datagrid)
Jul 16, 2010 at 4:45 PM
Edited Jul 16, 2010 at 5:01 PM
Having trouble formatting the code. You will find my msdn post here http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/49d17cfc-5bac-4f66-b432-618dcb03a0f3
Jul 18, 2010 at 1:07 AM

I have come across precisely the same DataGrid bug in WPF 4/ Visual Studio 2010.

Since it was a two column DataGrid in a scarcely used utility program, I just enabled all rows and did validation checking on both columns that prevented saving any changes for the conditions for which I had been disabling the row. Probably not much help to you.