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

GridViewDataColumn.IsReadOnly binding incorrectly

5 Answers 1099 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Louis
Top achievements
Rank 1
Louis asked on 01 Oct 2013, 04:20 PM
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:

<Window x:Class="RadGridViewTesting.MainWindow"
        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

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 02 Oct 2013, 05:45 AM
Hi,

In order to bind to "ReadOnly" on the "Employee" object, you should set the IsReadOnlyBinding property of RadGridView. Please check our online documentation for more information on the matter.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Louis
Top achievements
Rank 1
answered on 02 Oct 2013, 03:04 PM
Thank you Didie, but IsReadOnlyBinding appears to make the entire row (all columns) read-only, which is not what I need.

"When you set the IsReadOnlyBinding property of RadGridView - the full row becomes read only when the underlying property is True."

I need only the specific cell to be read-only.

At least IsReadOnlyBinding DOES appear to bind successfully to the ItemsSource object rather than the DataContext object; it seems that the IsReadOnly (which sounds like it should only affect the single cell) should do the same.
0
Dimitrina
Telerik team
answered on 03 Oct 2013, 06:58 AM
Hello,

Thank you for clarifying.

In order to have single cells being read only, you will need to apply a CellStyleSelector for the columns and return a Style targeting GridViewCell. 

You can check our online documentation on how to use a CellStyleSelector
here. The "Style Selectors -> Cells" WPF demo would also be helpful. 

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Louis
Top achievements
Rank 1
answered on 03 Oct 2013, 03:31 PM
Thanks again Didie. I actually achieved this using data triggers as described in the link in my first post. At least this keeps it within the XAML.

Are there any plans to correct IsReadOnly to bind to the ItemsSource objects instead of the DataContext?

Thanks again,
Louis
0
Dimitrina
Telerik team
answered on 04 Oct 2013, 07:59 AM
Hi Louis,

If you set the IsReadOnlyBinding (not IsReadOnly) property of the GridViewDataColumn - then only specific cells of that column will be affected. For more information you can check our online documentation here. The property should be available with the business objects bound to RadGridView.

On the other hand if you set the IsReadOnly property, then it should be available with the DataContext object.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Louis
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Louis
Top achievements
Rank 1
Share this question
or