Setting IsReadOnlyBinding breaks DataMemberBinding to Datatable

5 posts, 0 answers
  1. Lorentz
    Lorentz avatar
    3 posts
    Member since:
    Sep 2013

    Posted 12 Jun 2015 Link to this post

    I have stumbled upon an issue where I only want the user to be able to edit the lower half of a matrix (It is a matrix that is symmetrical across the diagonal). I am creating a datatable based on a 2d array. 

     the grid is defined like this:

    <telerik:RadGridView x:Name="matrixView" ItemsSource="{Binding MatrixTable}"
                                 CanUserDeleteRows="False" CanUserInsertRows="False"
                                 CanUserReorderColumns="False" CanUserSortColumns="False"
                                 ShowGroupPanel="False"
                                 GridLinesVisibility="Both"
                                 SelectionUnit="Cell"
                                 Width="AUto" Height="Auto">
    </telerik:RadGridView>

    And then I have the following viewmodel:

     

    public class MatrixViewModel
        {
            public DataTable MatrixTable { get; set; }
     
            public MatrixViewModel(double[,] matrix)
            {
                this.MatrixTable = BuildTable(matrix);
            }
     
            private DataTable BuildTable(double[,] matrix)
            {
                var table = new DataTable();
     
                table.Columns.Add("Name", typeof(string));
                for (int col = 0; col < matrix.GetLength(0); col++)
                {
                    table.Columns.Add(col.ToString(), typeof(double));
                }
     
                for (int i = 0; i < matrix.GetLength(0); i++)
                {
                    var row = table.NewRow();
                    row[0] = i.ToString();
     
                    for (int j = 0; j <= i; j++)
                    {
                        row[j + 1] = matrix[i, j];
                    }
                    table.Rows.Add(row);
                }
                return table;
            }
        }

    NullvalueToBoolean is defined as follows:

    [ValueConversion(typeof(object), typeof(Boolean))]
        public class NullValueToBoolean : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value == null;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    And finally the codebehind:

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new MatrixViewModel(new double[,]{
                    {1,0,0},
                    {0.25,1,0},
                    {0.25,0.75,1}
                });
                this.matrixView.AutoGeneratingColumn += matrixView_AutoGeneratingColumn;
            }
     
            void matrixView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
            {
                var column = (GridViewDataColumn)e.Column;
                column.IsFilterable = false;
     
                if (column.DataType == typeof(double?))
                {
                    column.IsReadOnlyBinding = new Binding(column.DataMemberBinding.Path.Path)
                    {
                        Converter = new NullValueToBoolean()
                    };
                }
                else
                {
                    column.IsReadOnly = true;
                }
            }
        }

     The code produces the following output when I try to edit one of the cells that are not readonly:
    System.Windows.Data Error: 40 : BindingExpression path error: '0' property not found on 'object' ''DataRow' (HashCode=9659819)'. BindingExpression:Path=0; DataItem='DataRow' (HashCode=9659819); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

    However, if I remove the code that sets the ReadOnlyBinding, editing works fine. Do you have a suggestion on how to fix/avoid this issue? Maybe there is a better way to solve this problem with the Telerik UI for WPF?

  2. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 17 Jun 2015 Link to this post

    Hi Lorentz,

    Thank you for the code provided.

    However, with it I am not able to reproduce such error. Can you please share some information on which assemblies version you are using?

    Best Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Lorentz
    Lorentz avatar
    3 posts
    Member since:
    Sep 2013

    Posted 17 Jun 2015 in reply to Stefan Link to this post

    Certainly, the telerik controls are version 2015.1.401.45  It is built for .net 4.5

    a full list of loaded assemblies can be found at http://pastebin.com/Sn5ds8h9

    sysinfo for the machine on which the error occurs: http://pastebin.com/tP5cDNi2

    The entire solution in which the error occurs can be found here: https://onedrive.live.com/redir?resid=8237EF9963C1295C!60134&authkey=!AJ-fHXeA6QUSPtg&ithint=file%2czip

  4. Lorentz
    Lorentz avatar
    3 posts
    Member since:
    Sep 2013

    Posted 17 Jun 2015 in reply to Lorentz Link to this post

    I accidentally swapped the link url and text attributes in the previous post, and found no way to edit it after it was posted. the correct link for the solution file is 

    https://onedrive.live.com/redir?resid=8237EF9963C1295C!60134&authkey=!AJ-fHXeA6QUSPtg&ithint=file%2czip

  5. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 17 Jun 2015 Link to this post

    Hi Lorentz,

    I am not able to open the referred solution either because you have removed it, or due to the fact that I do not have permissions for that. Would it be possible for you to send the solution in a new support thread, as this would make further communication and modifications of the project easier?

    Best Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top