GridViewDataColumn.IsReadOnly binding incorrectly

6 posts, 0 answers
  1. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 01 Oct 2013 Link to this post

    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
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 02 Oct 2013 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 02 Oct 2013 Link to this post

    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.
  5. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 03 Oct 2013 Link to this post

    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 >>
  6. Louis
    Louis avatar
    83 posts
    Member since:
    Aug 2013

    Posted 03 Oct 2013 Link to this post

    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
  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 04 Oct 2013 Link to this post

    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 >>
Back to Top
UI for WPF is Visual Studio 2017 Ready