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

WPF RadGridView Q3 2011 and DynamicObject.TrySetMember to MVVM Inherits class

5 Answers 224 Views
GridView
This is a migrated thread and some comments may be shown as answers.
JORGE
Top achievements
Rank 1
JORGE asked on 20 Jan 2012, 05:57 PM

Hello,


I’m running into a problem with WPF telerik:RadGridView Q3 2011 and DynamicObject

1) I use DynamicObject and pattern MVVM and After edit data in grid, the “DynamicObject.TrySetMember(...)” is never called by telerik:RadGridView.
Note, with Telerik WPF Q1 2011 it work fine!

2) If use “Inherits” class, the telerik:RadGridView see and use only the properties of base class
for example I have one property named “UpperName” inside one class Inherited from another class and “UpperName” is never displayed by telerik:RadGridView

Thanks for any help!

Ps: I use Telerik WPF 2011.3.1316.40 for .NET 4 , Visual Studio 2010 (x86) , Windows 7 (x64)


File : MainWindow.xaml :

<Window x:Class="MainWindow"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="412" Width="629" xmlns:my="clr-namespace:System;assembly=mscorlib">
        <Grid>
        <telerik:RadGridView  AutoGenerateColumns="False" ItemsSource="{Binding}" Name="DataGridList" Margin="0,0,0,49">     
             
           <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Width="*" Header="Upper Name"  DataMemberBinding="{Binding Path=UpperName}"   />
                <telerik:GridViewDataColumn Width="*" Header="Product Name"  DataMemberBinding="{Binding Path=Name,Mode=TwoWay}"   />
                <telerik:GridViewDataColumn Width="*" Header="Product ID"     DataMemberBinding="{Binding Path=ID,Mode=TwoWay}"   />               
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
 
        <Button Content="Here TrySetMember is OK" Height="43" HorizontalAlignment="Left" Margin="12,0,0,0" Name="Button1" VerticalAlignment="Bottom" Width="267" />
         
    </Grid>
</Window>

File : MainWindow.xaml.vb :

Imports System.Collections.ObjectModel
 
Class MainWindow
 
    Private _ViewProducts As New ObservableCollection(Of VM_Product)
 
    Public Sub New()
 
        ' This call is required by the designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        Dim i As Integer
        Dim p As VM_Product
        Dim MyObject As Object
        For i = 0 To 10
            p = New VM_Product
 
            _ViewProducts.Add(p)
 
            'Here TrySetMember is OK !
            MyObject = _ViewProducts.Item(i)
            MyObject.ID = i
            MyObject.Name = "Name " & i
        Next
 
        'But : With telerik:RadGridView TrySetMember is never called after input data from telerik:RadGridView
        Me.DataGridList.BeginInit()
        Me.DataGridList.DataContext = _ViewProducts
        Me.DataGridList.EndInit()
    End Sub
 
    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        'Here TrySetMember is OK !
        Dim MyObject As Object = _ViewProducts.Item(0)
        MyObject.Name = TimeOfDay & " Changed by System.Dynamic TrySetMember "
    End Sub
End Class



Very simple Dynamic ViewModel inherits from System DynamicObject
File : VM_Dynamic.vb

Imports System.Dynamic
Imports System.ComponentModel
Imports System.Reflection
 
Public MustInherit Class VM_Dynamic
    Inherits DynamicObject
    Implements INotifyPropertyChanged
 
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Private _Entity As Object
 
    Public Property Entity() As Object
        Get
            Entity = _Entity
        End Get
        Set(value As Object)
            _Entity = value
        End Set
    End Property
 
    Public Overrides Function TryGetMember(ByVal binder As System.Dynamic.GetMemberBinder, ByRef result As Object) As Boolean
        Dim nProperty As PropertyInfo = _Entity.GetType().GetProperty(binder.Name)
 
        If nProperty Is Nothing OrElse nProperty.CanRead = False Then
            result = Nothing
            Return False
        End If
 
        result = nProperty.GetValue(_Entity, Nothing)
        Return True
    End Function
 
    Public Overrides Function TrySetMember(ByVal binder As System.Dynamic.SetMemberBinder, ByVal value As Object) As Boolean
        Dim nProperty As PropertyInfo = _Entity.GetType().GetProperty(binder.Name)
        If nProperty Is Nothing Or nProperty.CanRead = False Then Return False
 
        If nProperty.GetValue(Entity, Nothing) <> value AndAlso nProperty.CanWrite = True Then
            nProperty.SetValue(Entity, value, Nothing)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(binder.Name))
        End If
 
        Return True
    End Function
 
    Protected Overridable Sub OnPropertyChanged(ByRef propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
 
End Class



Model "Product"
File : Product.vb

Public Class Product
    Private _ID As Integer
    Private _Name As String
 
    Public Property ID() As Integer
        Get
            ID = _ID
        End Get
        Set(value As Integer)
            _ID = value
        End Set
    End Property
 
    Public Property Name() As String
        Get
            Name = _Name
        End Get
        Set(value As String)
            _Name = value
        End Set
    End Property
 
End Class



"Product" View Model , inherits class "VM_Dynamic"
File : VM_Product.vb

Imports System.ComponentModel
  
Public Class VM_Product
    Inherits VM_Dynamic
 
    'This property is never called by telerik:RadGridView from XAML !!!
    Public ReadOnly Property UpperName() As String
        Get
            UpperName = StrConv(Me.Entity.Name, VbStrConv.Uppercase)
        End Get
    End Property
 
    Public Sub New()
        Me.Entity = New Product
    End Sub
End Class









5 Answers, 1 is accepted

Sort by
0
Franck
Top achievements
Rank 1
answered on 10 Dec 2012, 05:16 PM
Hello, I have the same issue.

When a cell is modified, the getter is called and the old value replaces the new one.

Any idea of what happens ?

Best regards,

Franck
0
Vlad
Telerik team
answered on 11 Dec 2012, 07:23 AM
Hi,

 Please open support ticket and send us small example project demonstrating your scenario and the problem. 

All the best,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Combinations
Top achievements
Rank 1
answered on 19 Mar 2013, 12:38 PM
Vlad:
I've got the same problem as Franck (not being able to use DynamicObject bound in a GridViewDataColumn). Did you solve the problem? Otherwise I can send a example project.

I'm using 2013Q1 components.
0
Combinations
Top achievements
Rank 1
answered on 20 Mar 2013, 10:30 AM
I found out why it didn't work for me:
I hadn't implemented the method GetDynamicMemberNames() in my DynamicObject class. It seems as if that is required.
0
Dimitrina
Telerik team
answered on 20 Mar 2013, 10:52 AM
Hello,

Thank you for sharing the solution with the community. I will also check the sample you sent in your another ticket.

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
JORGE
Top achievements
Rank 1
Answers by
Franck
Top achievements
Rank 1
Vlad
Telerik team
Combinations
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or