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

Binding headache

7 Answers 107 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 14 Mar 2013, 03:16 PM
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?

7 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 15 Mar 2013, 06:55 AM
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.

0
Charles
Top achievements
Rank 1
answered on 15 Mar 2013, 01:42 PM
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

0
Charles
Top achievements
Rank 1
answered on 15 Mar 2013, 02:13 PM
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.    
0
Nedyalko Nikolov
Telerik team
answered on 20 Mar 2013, 07:44 AM
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.

0
Charles
Top achievements
Rank 1
answered on 22 Mar 2013, 03:23 PM
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
0
Charles
Top achievements
Rank 1
answered on 22 Mar 2013, 03:56 PM
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!
0
Nedyalko Nikolov
Telerik team
answered on 26 Mar 2013, 02:05 PM
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.

Tags
GridView
Asked by
Charles
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Charles
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or