I have a DataGrid
like this:
<DataGrid Name="studentGrid" Grid.Row="3" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding Students, Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Header="Student's name" Binding="{Binding StudentName}" />
<DataGridTextColumn IsReadOnly="True" Header="Student's username" Binding="{Binding StudentUserName}" />
<DataGridTextColumn IsReadOnly="True" Header="Date of application" Binding="{Binding DateOfApplication}" />
<DataGridTextColumn IsReadOnly="False" Header="Student's grade" Binding="{Binding StudentGrade}"/>
</DataGrid.Columns>
</DataGrid>
This is working right now, however, I'd like to add some functionality, namely I'd like the Student's grade
column to become read only IF the grade is above 1. I keep seeing solutions like this, where people use a DataGridVIEW programatically. However, I can't seem to add a DataGridVIEW to my XAML, only a DataGrid. And when I'm trying to do something like:
public MainWindow()
{
foreach (var row in studentGrid)
{
if (row[3] > 1) {
row[3].isReadOnly = false;
}
}
InitializeComponent();
}
I can't, since DataGrid doesn't have an enumerator. Nor can I access its rows with something like studentGrid.Row
.
So my questions would be, what's the difference in usage of DataGrid vs DataGridView, and how can I add an if-else validation to make certain rows in a column of a DataGrid editable, but others not?
Thank you!
UPDATE
So I've created a converter, but I'm having trouble using it. I'm trying to reference it in the XAML:
<Window x:Class="TanulmanyiRendszer.Admin.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:viewModel="clr-namespace:TanulmanyiRendszer.Admin.ViewModel"
Title="Courses" Height="600" Width="500">
<Window.Resources>
<viewModel:GradeToReadOnlyConverter x:Key="converter" />
</Window.Resources>
But I get an error:
The name "GradeToReadOnlyConverter" does not exist in the namespace "clr-namespace:TanulmanyiRendszer.Admin.ViewModel".
Even though the converter clearly exists in that namespace:
namespace TanulmanyiRendszer.Admin.ViewModel
{
public class GradeToReadOnlyConverter : IValueConverter
{