-1

I want to convert some integers to a DataGrid column's ReadOnly value. For this, I'm doing the following:

namespace TanulmanyiRendszer.Admin.ViewModel
{
    public class GradeToReadOnlyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Boolean IsReadOnly = (Int32.Parse((String)value) < 2) ? true : false;
            return IsReadOnly;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}

XAML View

<Window x:Class="TanulmanyiRendszer.Admin.View.MainWindow"
    <!-- ETC -->
    xmlns:viewModel="clr-namespace:TanulmanyiRendszer.Admin.ViewModel"
    Title="Courses" Height="600" Width="500">
        <Window.Resources>
            <viewModel:GradeToReadOnlyConverter x:Key="converter" />
        </Window.Resources>
        <!-- ETC -->
        <DataGrid Name="studentGrid" Grid.Row="3" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding Students, Mode=TwoWay}" >
            <DataGrid.Columns>
                <DataGridTextColumn IsReadOnly="{Binding StudentGrade, Converter={StaticResource converter}}" Header="Student's grade" Binding="{Binding StudentGrade}"/>
            </DataGrid.Columns>
        </DataGrid>
</Window>

This, however, doesn't work at all. The converter never gets called. What am I missing here?

ColinM
  • 2,622
  • 17
  • 29
lte__
  • 7,175
  • 25
  • 74
  • 131
  • 2
    But `Binding="{Binding StudentGrade}"` is working? – Clemens May 17 '17 at 08:37
  • @Clemens Yes, exaclty. The grades show up, but I'm able to edit them regardless of value. – lte__ May 17 '17 at 08:38
  • Check output window, you must be getting this error : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=StudentGrade; DataItem=null; target element is 'DataGridTextColumn' (HashCode=15006601); target property is 'IsReadOnly' (type 'Boolean'). Reason and Solution for same can be found here : http://stackoverflow.com/questions/7660967/wpf-error-cannot-find-governing-frameworkelement-for-target-element – Naresh Ravlani May 17 '17 at 08:44

3 Answers3

1
<DataGridTextColumn Header="Student's grade" Binding="{Binding StudentGrade}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="IsHitTestVisible" Value="{Binding StudentGrade, Converter={StaticResource converter}}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

Try to change style of DataGridCell. Setter Property could be 'IsHitTestVisible' or ' IsEnabled'

mm8
  • 163,881
  • 10
  • 57
  • 88
KMCho
  • 46
  • 3
0

This, however, doesn't work at all.

This is expected behaviour in this scenario. The reason behind this is Datagrid Columns are not part of Visual Tree and that is why Datagrid Columns are not connected to Datacontext of Datagrid. So binding with the IsReadOnly property of Datagrid Column won't work. A good solution (hack/Workaround) for this scenario can be found here.

Let me know if you need any help with the solution of provided link.

Community
  • 1
  • 1
Naresh Ravlani
  • 1,600
  • 13
  • 28
0

What am I missing here?

The fact that a DataGridTextColumn doesn't inherit any DataContext which means that you cannot bind its IsReadOnly property directly to the StudentGrade source property.

Please refer to @Thomas Levesque's blog post for more information about this and how you could possibly solve your issue by using a BindingProxy class that inherits from Freezable and captures the DataContext: https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/.

It will be easier to simply define a custom EditingElementStyle for the column though. Try this:

<DataGridTextColumn IsReadOnly="{Binding StudentGrade, Converter={StaticResource converter}}"
                    Header="Student's grade" Binding="{Binding StudentGrade}">
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding StudentGrade, Converter={StaticResource converter}}" Value="True">
                    <Setter Property="BorderThickness" Value="0" />
                    <Setter Property="IsEnabled" Value="False" />
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="TextWrapping" Value="Wrap" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
mm8
  • 163,881
  • 10
  • 57
  • 88