This is a migrated thread and some comments may be shown as answers.

Using IsReadOnlyBinding with a View Model property

6 Answers 571 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Brian
Top achievements
Rank 1
Brian asked on 02 May 2012, 09:37 PM
Getting a NullReferenceException when trying to use IsReadOnlyBinding on a GridViewDataColumn.

LabDetails is a collection of "LabDetail".  But I do not have a property on LabDetail that identifies whether it is read only or not.  I want to use a property on the ViewModel "SelectedLabDetailEditable" which does identify whether that particular LabDetail should be editable.

So either I have a problem with my binding to that property, or it would seem that this IsReadOnlyBinding doesn't support using properties that are not on the entity that the Grid is bound to.


<telerik:RadGridView AutoGenerateColumns="False"
             ItemsSource="{Binding LabDetails}"
             SelectedItem="{Binding SelectedLabDetail}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn Header="Result"
                         DataMemberBinding="{Binding TokenValue}"
                         IsReadOnlyBinding="{Binding DataContext.SelectedLabDetailEditable, ElementName=LayoutRoot}" />
    </telerik:RadGridView.Columns>

</telerik:RadGridView>


It seems that my binding statement is correct, I bound that to a simple textblock and the property is updated and correct as my SelectedLabDetail changes:

<TextBlock Text="{Binding DataContext.SelectedLabDetailEditable, ElementName=LayoutRoot}" />


The stack trace of the exception is:

at Telerik.Windows.Controls.GridViewBoundColumnBase.EvaluateIsReadOnlyState(Object item)
at Telerik.Windows.Controls.GridViewBoundColumnBase.CanEdit(Object item)
at Telerik.Windows.Controls.GridView.GridViewDataControl.BeginEdit(GridViewCell gridViewCell, RoutedEventArgs editingEventArgs)
at Telerik.Windows.Controls.GridView.GridViewDataControl.OnCellMouseDown(GridViewCell cell, MouseButtonEventArgs args)
at Telerik.Windows.Controls.GridView.GridViewCell.OnMouseLeftButtonDown(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)



6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 03 May 2012, 07:43 AM
Hello Brian,

That would be the expected behavior since the data context of the columns is the corresponding data item, not your view model. And you need to have the required property defined in your business object. If you have that property declared in your view model, you can set it as grid's level, not column's:

<telerik:RadGridView x:Name="radGridView" IsReadOnlyBinding="{Binding IsActive}" />

You can take a look at this article for further reference. 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Brian
Top achievements
Rank 1
answered on 03 May 2012, 04:06 PM

"since the data context is the corresponding data item, not your view model"

Except that the ElementName allows me to change the context of the binding. For example, we use a property of the view
model to control the visibility of certain columns:

<telerik:GridViewDataColumn
         Header="Created"
         DataMemberBinding="{Binding Created, StringFormat='{}{0:M/d/yyyy'}"
         IsVisible="{Binding DataContext.LabRequisitionGridExpanded, ElementName=LayoutRoot}" />

In this case, same situation, binding to a property of the view model.  So it would seem that the IsReadOnlyBinding doesn't fully support binding.

We need to control the readonly state of the column on a per row basis, not for the whole grid.  Certain rows are editable while others are not, and it is a property of the view model where that editability is determined, not in the entity.

0
Maya
Telerik team
answered on 03 May 2012, 04:36 PM
Hi Brian,

Probably I am missing something, but if you define IsReadOnlyBinding at grid level, you will get editable/ read-only rows. Isn't that the behavior you require ? Or you want to have editable and non-editable cells only for a particular column?  

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Brian
Top achievements
Rank 1
answered on 03 May 2012, 04:37 PM
Oh I didn't realize - I thought it would make the whole grid readonly or not readonly.
I will try that.
Thanks!
0
Brian
Top achievements
Rank 1
answered on 03 May 2012, 09:52 PM
I tried using the IsReadOnlyBinding at the Grid element, but still getting a null reference exception:

XAML:

<telerik:RadGridView Grid.Row="1"
             AutoGenerateColumns="False"
             ItemsSource="{Binding LabDetails}"
             SelectedItem="{Binding SelectedLabDetail, Mode=TwoWay}"
             ShowGroupPanel="False"
             IsReadOnlyBinding="{Binding SelectedLabDetailEditable}"
             >


Exception:

Message Trace:
NullReferenceException: Object reference not set to an instance of an object.


Stack Trace:
NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.GridViewBoundColumnBase.EvaluateIsReadOnlyState(Object item)
   at Telerik.Windows.Controls.GridViewBoundColumnBase.CanEdit(Object item)
   at Telerik.Windows.Controls.GridView.GridViewDataControl.BeginEdit(GridViewCell gridViewCell, RoutedEventArgs editingEventArgs)
   at Telerik.Windows.Controls.GridView.GridViewDataControl.OnCellMouseDown(GridViewCell cell, MouseButtonEventArgs args)
   at Telerik.Windows.Controls.GridView.GridViewCell.OnMouseLeftButtonDown(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)


0
Maya
Telerik team
answered on 04 May 2012, 08:34 AM
Hi Brian,

I really need to apologize on this as it was totally my mistake that I realize only after I created a sample project for reproducing the exception you reported. Indeed, the data context of the grid is the view model, the data context of the column is the corresponding item, but IsReadOnlyBinding is implemented so that it expects property from your business object (I am attaching a sample project for a reference).
The idea is that each item can be defined as either read-only or editable (base on the corresponding property in the business object implementation). If there were only a single property from your view model, then all items would be with equal state - read-only or editable. 
Please accept my sincere apology for the misunderstanding.

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Brian
Top achievements
Rank 1
Answers by
Maya
Telerik team
Brian
Top achievements
Rank 1
Share this question
or