I have stumbled upon an issue where I only want the user to be able to edit the lower half of a matrix (It is a matrix that is symmetrical across the diagonal). I am creating a datatable based on a 2d array.
the grid is defined like this:
<
telerik:RadGridView
x:Name
=
"matrixView"
ItemsSource
=
"{Binding MatrixTable}"
CanUserDeleteRows
=
"False"
CanUserInsertRows
=
"False"
CanUserReorderColumns
=
"False"
CanUserSortColumns
=
"False"
ShowGroupPanel
=
"False"
GridLinesVisibility
=
"Both"
SelectionUnit
=
"Cell"
Width
=
"AUto"
Height
=
"Auto"
>
</
telerik:RadGridView
>
And then I have the following viewmodel:
public
class
MatrixViewModel
{
public
DataTable MatrixTable {
get
;
set
; }
public
MatrixViewModel(
double
[,] matrix)
{
this
.MatrixTable = BuildTable(matrix);
}
private
DataTable BuildTable(
double
[,] matrix)
{
var table =
new
DataTable();
table.Columns.Add(
"Name"
,
typeof
(
string
));
for
(
int
col = 0; col < matrix.GetLength(0); col++)
{
table.Columns.Add(col.ToString(),
typeof
(
double
));
}
for
(
int
i = 0; i < matrix.GetLength(0); i++)
{
var row = table.NewRow();
row[0] = i.ToString();
for
(
int
j = 0; j <= i; j++)
{
row[j + 1] = matrix[i, j];
}
table.Rows.Add(row);
}
return
table;
}
}
NullvalueToBoolean is defined as follows:
[ValueConversion(
typeof
(
object
),
typeof
(Boolean))]
public
class
NullValueToBoolean : IValueConverter
{
public
object
Convert(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
{
return
value ==
null
;
}
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
{
throw
new
NotImplementedException();
}
}
And finally the codebehind:
public
partial
class
MainWindow : Window
{
public
MainWindow()
{
InitializeComponent();
this
.DataContext =
new
MatrixViewModel(
new
double
[,]{
{1,0,0},
{0.25,1,0},
{0.25,0.75,1}
});
this
.matrixView.AutoGeneratingColumn += matrixView_AutoGeneratingColumn;
}
void
matrixView_AutoGeneratingColumn(
object
sender, GridViewAutoGeneratingColumnEventArgs e)
{
var column = (GridViewDataColumn)e.Column;
column.IsFilterable =
false
;
if
(column.DataType ==
typeof
(
double
?))
{
column.IsReadOnlyBinding =
new
Binding(column.DataMemberBinding.Path.Path)
{
Converter =
new
NullValueToBoolean()
};
}
else
{
column.IsReadOnly =
true
;
}
}
}
The code produces the following output when I try to edit one of the cells that are not readonly:
System.Windows.Data Error: 40 : BindingExpression path error: '0' property not found on 'object' ''DataRow' (HashCode=9659819)'. BindingExpression:Path=0; DataItem='DataRow' (HashCode=9659819); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')
However, if I remove the code that sets the ReadOnlyBinding, editing works fine. Do you have a suggestion on how to fix/avoid this issue? Maybe there is a better way to solve this problem with the Telerik UI for WPF?