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

6 posts, 0 answers
  1. JORGE
    JORGE avatar
    1 posts
    Member since:
    Jun 2009

    Posted 20 Jan 2012 Link to this post

    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









  2. Franck
    Franck avatar
    2 posts
    Member since:
    Nov 2012

    Posted 10 Dec 2012 Link to this post

    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
  3. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 11 Dec 2012 Link to this post

    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.

  4. Combinations
    Combinations avatar
    14 posts
    Member since:
    Jun 2012

    Posted 19 Mar 2013 Link to this post

    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.
  5. Combinations
    Combinations avatar
    14 posts
    Member since:
    Jun 2012

    Posted 20 Mar 2013 Link to this post

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

    Posted 20 Mar 2013 Link to this post

    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.

Back to Top