I'm trying to bind the IsReadOnly property on a GridViewDataColumn, but it appears to be binding to the DataContext of the GridView instead of the context created by the ItemsSource. My XAML:
Simple code behind to create the ViewModel DataContext:
And finally the ViewModel:
This results in the following output message:
System.Windows.Data Error: 40 : BindingExpression path error: 'ReadOnly' property not found on 'object' ''ViewModel' (HashCode=24397644)'. BindingExpression:Path=ReadOnly; DataItem='ViewModel' (HashCode=24397644); target element is 'GridViewDataColumn' (HashCode=15286079); target property is 'IsReadOnly' (type 'Boolean')
It should be looking for "ReadOnly" on the "Employee" object (same object it's binding the DataMemberBinding to) rather than the "ViewModel" object. The only work-around I've found is to set up DataTriggers for every column I need to be able to disable (in my case several) as mentioned in this post, but that certainly isn't elegant. Is there another known solution?
Thanks,
Louis
<
Window
x:Class
=
"RadGridViewTesting.MainWindow"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
Title
=
"MainWindow"
Height
=
"350"
Width
=
"525"
>
<
Grid
>
<
telerik:RadGridView
x:Name
=
"radGridView"
AutoGenerateColumns
=
"False"
ItemsSource
=
"{Binding Path=EmployeeList}"
>
<
telerik:RadGridView.Columns
>
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding Path=FirstName}"
Header
=
"First Name"
UniqueName
=
"FirstName"
IsReadOnly
=
"{Binding Path=ReadOnly}"
/>
</
telerik:RadGridView.Columns
>
</
telerik:RadGridView
>
</
Grid
>
</
Window
>
Simple code behind to create the ViewModel DataContext:
namespace
RadGridViewTesting
{
public
partial
class
MainWindow : Window
{
public
MainWindow()
{
InitializeComponent();
DataContext =
new
ViewModel();
}
}
}
And finally the ViewModel:
namespace
RadGridViewTesting
{
public
class
Employee
{
public
string
FirstName {
get
;
set
; }
public
bool
ReadOnly {
get
;
set
; }
}
class
ViewModel
{
List<Employee> _myList;
public
ViewModel()
{
_myList =
new
List<Employee>();
_myList.Add(
new
Employee() { FirstName =
"John"
, ReadOnly =
true
});
_myList.Add(
new
Employee() { FirstName =
"Jane"
, ReadOnly =
false
});
_myList.Add(
new
Employee() { FirstName =
"Jack"
, ReadOnly =
true
});
}
public
List<Employee> EmployeeList {
get
{
return
_myList; } }
}
}
This results in the following output message:
System.Windows.Data Error: 40 : BindingExpression path error: 'ReadOnly' property not found on 'object' ''ViewModel' (HashCode=24397644)'. BindingExpression:Path=ReadOnly; DataItem='ViewModel' (HashCode=24397644); target element is 'GridViewDataColumn' (HashCode=15286079); target property is 'IsReadOnly' (type 'Boolean')
It should be looking for "ReadOnly" on the "Employee" object (same object it's binding the DataMemberBinding to) rather than the "ViewModel" object. The only work-around I've found is to set up DataTriggers for every column I need to be able to disable (in my case several) as mentioned in this post, but that certainly isn't elegant. Is there another known solution?
Thanks,
Louis