Welcome to the Xceed Community | Help
Community Search  
More Search Options

Creating a custom detail description

Sort Posts: Previous Next
  •  10-08-2008, 4:10 PM Post no. 28144

    Creating a custom detail description

    The following example demonstrates how to create and use a custom detail description that handles LINQ detail relations, which are provided by properties to which the AssociationAttribute is applied.

    VB.NET

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports Xceed.Wpf.DataGrid
    Imports System.Reflection
    Imports System.Data.Linq.Mapping
    Imports System.Diagnostics
    Imports System.Collections

    Namespace Xceed.Wpf.Documentation
      Public Class LinqToSqlDetailDescription
                   Inherits DataGridDetailDescription

        Protected Overrides Function GetDetailsForParentItem( ByVal parentCollectionView As DataGridCollectionView, _
                                                              ByVal parentItem As Object ) As IEnumerable
          Dim parentItemType As Type = parentItem.GetType()
          Dim foundProperty As PropertyInfo = Nothing
          Dim properties() As PropertyInfo = parentItemType.GetProperties()
          Dim propertyInfo As PropertyInfo

          For Each propertyInfo In properties
            Dim attributes() As Object = propertyInfo.GetCustomAttributes( Type.GetType( AssociationAttribute ), False)

            If attributes.GetLength( 0 ) = 0 Then
              continue
            End If

            Dim associationAttribute As AssociationAttribute = CType( attributes( 0 ), AssociationAttribute )

            If associationAttribute.Name = Me.RelationName Then
              foundProperty = propertyInfo
              Exit Property
            End If
          Next

          If foundProperty Is Nothing Then
            Return New Object()
          Else
            Dim details As Object = foundProperty.GetValue( parentItem, Nothing )
            Dim detailsType As Type = details.GetType()
            Dim getNewBindingList As MethodInfo = detailsType.GetMethod( "GetNewBindingList" )

            Return CType( getNewBindingList.Invoke( details, Nothing), IEnumerable )
          End If
        End Function
      End Class
    End Namespace

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Xceed.Wpf.DataGrid;
    using System.Reflection;
    using System.Data.Linq.Mapping;
    using System.Diagnostics;
    using System.Collections;

    namespace Xceed.Wpf.Documentation
    {
     public class LinqToSqlDetailDescription: DataGridDetailDescription
     {
       protected override IEnumerable GetDetailsForParentItem( DataGridCollectionView parentCollectionView, object parentItem )
       {
         Type parentItemType = parentItem.GetType();
         PropertyInfo foundProperty = null;
        
         PropertyInfo[] properties = parentItemType.GetProperties();
         foreach( PropertyInfo propertyInfo in properties )
         {
           object[] attributes = propertyInfo.GetCustomAttributes( typeof( AssociationAttribute ), false );

           if( attributes.GetLength( 0 ) == 0 )
             continue;

           AssociationAttribute associationAttribute = ( AssociationAttribute )attributes[ 0 ];
           if( associationAttribute.Name == this.RelationName )
           {
             foundProperty = propertyInfo;
             break;
           }
         }

         if( foundProperty == null )
         {
           return new object[] { };
         }
         else
         {
           object details = foundProperty.GetValue( parentItem, null );
           Type detailsType = details.GetType();
           MethodInfo getNewBindingList = detailsType.GetMethod( "GetNewBindingList" );

           return ( IEnumerable )getNewBindingList.Invoke( details, null );
         }
       }
     }<

    View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.