Binding headache

8 posts, 0 answers
  1. Charles
    Charles avatar
    41 posts
    Member since:
    Mar 2012

    Posted 14 Mar 2013 Link to this post

    I don't know if I'm missing something obvious, but if anyone has any ideas I'd be grateful.  Feel like I'm banging my head against a wall!

    My requirement is that I need a column with all the usual stuff that GridViewDataColumn usually gives you - display, edit, filter, sort, group etc.  But I can't work out how to get a DataMemberBinding value that will do what I want.

    It probably makes the most sense if I explain the actual problem rather than trying to make one up, as it's fairly easy to understand.

    I have 3 'fixed' columns:

    Description (text)
    Value (decimal)
    Adjusted Value (decimal)

    I then have 0->n 'Adjustment' (decimal) columns that get dynamically added in between Value and Adjusted Value.  So the idea is that the Value + all Adjustment values = Adjusted Value

    So, the value I need to bind to is a decimal, but it requires a method to access it (e.g. 'GetValueForAdjustment(Adjustment adjustment)') and a method to set it (e.g. 'SetValueForAdjustment(Adjustment adjustment, decimal value)'). 

    Now I could probably set DataMemberBinding to use a ValueConverter which will get me the value, sorting, grouping etc.  But I'm not going to be able to edit it. 

    MultiBinding is the other option, but DataMemberBinding doesn't support MultiBinding.

    Anything glaring that I'm missing?

  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 15 Mar 2013 Link to this post

    Hello,

     Why not use dynamics to achieve your goal similar to this demo? The same example can be found in your local copy of our WPF examples. 

    Kind regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Charles
    Charles avatar
    41 posts
    Member since:
    Mar 2012

    Posted 15 Mar 2013 Link to this post

    Hi Vlad

    Thanks - this is a possible solution.  However, I think I've possibly found a bug.  The DataMemberBinding works fine for me, but IsReadOnlyBinding causes a NullRefrenceException to be thrown every time I try to edit.

    The binding from the item ViewModel I'm using for the Items is Adjustments.{id}.Value for DataMemberBinding and Adjustments.{id}.IsReadOnly for IsReadOnlyBinding.  Adjustments is a DynamicObject that returns another class with Value and IsReadOnly properties.

    It appears that it has trouble creating the access func from the binding when it gets to the dynamic property name.

    Can you confirm this is an issue?  As a temporary workaround, I can use an event handler to cancel the edit if IsReadOnly == true, but the binding would be neater.

    Charles

  4. Charles
    Charles avatar
    41 posts
    Member since:
    Mar 2012

    Posted 15 Mar 2013 Link to this post

    Also, if I make the root item the Dynamic Object (so the path is {id}.Value and {id.IsReadOnly}, then neither binding works due to errors creating the access funcs.

    In the way I set out above, I'm getting a first chance ArgumentException in Telerik.Windows.Data in PropertyPathDescriptor.GetPropertyDescriptor(...).  And sorting/filtering etc aren't working for that column.

    It seems there's a few issues with Dynamics floating about!  I may have to revert to the 'Converter' method and fudge the editor somehow.  Neither way is ideal.    
  5. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 20 Mar 2013 Link to this post

    Hello,

    I've tried to simulate the problem unfortunately to no avail. Maybe I've missed something.
    I'm attaching my sample project for a reference. Could you please let me know how to modify it in order to reproduce the error (or send me your project)?

    P.S. You'll have to open a separate support ticket if you want to attach any files.

    Kind regards,
    Nedyalko Nikolov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Charles
    Charles avatar
    41 posts
    Member since:
    Mar 2012

    Posted 22 Mar 2013 Link to this post

    Hi Nedyalko

    I can demo the first issue I referred to (with a nested DynamicObject causing a NullReferenceException on accessing the IsReadOnlyBinding).  I can't reproduce either of the issues I described in the last post with your example, though - I'll have to try harder!

    I'll attach the project for the first issue to a new ticket.

    Charles
  7. Charles
    Charles avatar
    41 posts
    Member since:
    Mar 2012

    Posted 22 Mar 2013 Link to this post

    Managed to reproduce one of the other two (DataMember Binding to DynamicProperty.PropertyOnReturnedObject fails to build access func).

    I will also included a second project that shows that NormalProperty.DynamicProperty.PropertyOnReturnedObject works fine... though I'm getting first chance handling of ArgumentExceptions in mine, but not in this sample. I haven't worked that out yet!
  8. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 26 Mar 2013 Link to this post

    Hello,

    I'm pasting my answer to your support ticket in order to be publicly available.

    Thank you for the sample project. I've managed to find the root of the problems.
    The problem with displaying and editing the content of a nested property of a dynamic object will be fixed with the next internal build. However the problem with "IsReadOnlyBinding" is not so trivial, since this binding is just a common way to create an expression Func<object, bool> where object is the underlying business object (or row's DataContext) and according to the result cell enters into edit mode or not. Unfortunately this approach does not work with dynamic objects, we will do our best in order to support such scenarios but I cannot commit with any specific date or release. For now I would suggest you to use a simple (not nested) property or use a converter similar to the following:

    Copy Code
    public class TestConverter : IValueConverter
        {
     
            #region IValueConverter Members
     
            public object Convert(object value, Type targetType, objectparameter, System.Globalization.CultureInfo culture)
            {
                MyDataRow dataRow = value as MyDataRow;
                if (dataRow != null)
                {
                    return dataRow["IsReadOnly"];
                }
                return value;
            }
     
            public object ConvertBack(object value, Type targetType, objectparameter, System.Globalization.CultureInfo culture)
            {
                return value;
            }
     
            #endregion
        }

    Copy Code
    <Grid.Resources>
                <local:TestConverter x:Key="testConverter" />
            </Grid.Resources>
            <telerik:RadGridView x:Name="radGridView"AutoGenerateColumns="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="Name"DataMemberBinding="{Binding DataRow.Name}"
                                                IsReadOnlyBinding="{Binding DataRow, Converter={StaticResource testConverter}}" />
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>

    Kind regards,
    Nedyalko Nikolov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top