0

Maybe I'm misunderstanding how to use an IValueConverter or data binding (which is likely), but I am currently trying to set the IsReadOnly property of a DataGridTextColumn depending on the value of a string. Here is the XAML:

<DataGridTextColumn Binding="{Binding Path=GroupDescription}" Header="Name"
                    IsReadOnly="{Binding Current,
                                 Converter={StaticResource currentConverter}}"/>

And here is my converter:

public class CurrentConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = value as string;
        if (s == "Current")
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Currently, the column is always editable, the converter seems to do nothing. Does anyone have some thoughts as to why this is happening?

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
mastur cheef
  • 185
  • 1
  • 3
  • 15
  • And what is the question/problem? – Szymon Nov 04 '13 at 21:14
  • 1
    Updated the end of the question, hopefully it isn't too vague... – mastur cheef Nov 04 '13 at 21:17
  • IsReadOnly isn't bindable. See my answer on http://stackoverflow.com/questions/18443063/bind-datagrid-textbox-enable-based-on-checkbox-property/18444724#18444724 – jamesSampica Nov 04 '13 at 21:24
  • @Shoe - `IsReadOnly` is bindable like any other DP. – Rohit Vats Nov 04 '13 at 21:27
  • @mastur - Where property `Current` lies? In your ViewModel class or underlying ItemsSource object? – Rohit Vats Nov 04 '13 at 21:29
  • @Rohit It is a DP but it is not a part of the visual tree, thus it does not receive notifications from source changes. [See this for more info](http://connect.microsoft.com/VisualStudio/feedback/details/530280/wpf-4-vs2010-datagrid-isreadonly-does-not-work-with-binding-to-boolean-property) – jamesSampica Nov 04 '13 at 21:31
  • @Shoe - Not lying in Visual Tree doesn't make DP non-bindable. You can always bind with it using `Freezable` object or by storing object in `Tag` and binding from there. Also check this link out [here](http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx). – Rohit Vats Nov 04 '13 at 21:35
  • @Rohit it is non-bindable at the cell level. Yes, you can find a way to bind it at the column level. That's not what this question asks for. – jamesSampica Nov 04 '13 at 21:37
  • @Rohit Current lies in the underlying ItemsSource object – mastur cheef Nov 04 '13 at 21:40
  • @Shoe I copied the code from your answer and it works! Except that I can only input numbers, no letters. Is there any reason why that is that I am missing? – mastur cheef Nov 04 '13 at 21:46
  • @Shoe - Statement seems legitimate now. – Rohit Vats Nov 04 '13 at 21:47

2 Answers2

0

Instead of using converter you can use DataTrigger to enable\disable DataGridCell:

<DataGridTextColumn Header="Name" Binding="{Binding GroupDescription}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Current}" Value="Current">
                    <Setter Property="TextBlock.IsEnabled" Value="False" />                                    
                </DataTrigger>                              
            </Style.Triggers>
        </Style>                       
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
0

The value of DataGridTextColumn's IsReadOnly property is a global value that will effects all cells. The individual cell doesn't has its own IsReadOnly property. Try create own DependencyProperty like this: http://blog.spencen.com/2009/04/25/readonly-rows-and-cells-in-a-datagrid.aspx

Aleksey
  • 1,299
  • 9
  • 13