Using IsReadOnlyBinding with a View Model property

7 posts, 0 answers
  1. Brian
    Brian avatar
    14 posts
    Member since:
    Oct 2010

    Posted 02 May 2012 Link to this post

    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)



  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 May 2012 Link to this post

    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 >>

  3. Brian
    Brian avatar
    14 posts
    Member since:
    Oct 2010

    Posted 03 May 2012 Link to this post

    "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.

  4. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 May 2012 Link to this post

    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 >>

  5. Brian
    Brian avatar
    14 posts
    Member since:
    Oct 2010

    Posted 03 May 2012 Link to this post

    Oh I didn't realize - I thought it would make the whole grid readonly or not readonly.
    I will try that.
    Thanks!
  6. Brian
    Brian avatar
    14 posts
    Member since:
    Oct 2010

    Posted 03 May 2012 Link to this post

    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)


  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 04 May 2012 Link to this post

    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 >>

Back to Top