Silverlight DataBinding Error

后端 未结 2 857
予麋鹿
予麋鹿 2021-01-16 17:22

I\'ve a GridView which has RowDetail. I want to each time user clicks on the rows get some detail from database, I use Telerik GridView

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-16 17:38

    The reason for this is that WPF's and Silverlights DataGrid columns live outside the logical tree and thus make it impossible to use a binding source specified using ElementName which is common when referencing ViewModel properties such as commands from within DataGrid Template Columns. For more information about this problem see: http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx

    The class below act's as glue between the column and the world around it. It was written for Silverlight's built-in DataGrid but should be easy enough to adapt it for the Telerik Grid. It can be used like this:

    
      
            
        
          
        
        
          
          
          
        
        
        
        
      
    
    
    public class DataGridShim : FrameworkElement
    {
      /// 
      /// Initializes a new instance of the  class.
      /// prepares the ParentDataGrid property for consumption by sibling elements in the DataTemplate
      /// 
      public DataGridShim()
      {
        Loaded += (s, re) =>
        {
          ParentDataGrid = GetContainingDataGrid(this);
        };
      }
    
      /// 
      /// Gets or sets the parent data grid.
      /// 
      /// 
      /// The parent data grid.
      /// 
      public DataGrid ParentDataGrid { get; protected set; }
    
      /// 
      /// Walks the Visual Tree until the DataGrid parent is found and returns it
      /// 
      /// The value.
      /// The containing datagrid
      private static DataGrid GetContainingDataGrid(DependencyObject value)
      {
        if (value != null)
        {
          DependencyObject parent = VisualTreeHelper.GetParent(value);
          if (parent != null)
          {
            var grid = parent as DataGrid;
            if (grid != null)
              return grid;
    
            return GetContainingDataGrid(parent);
          }
    
          return null;
        }
    
        return null;
      }
    }
    

提交回复
热议问题