2013-08-16 87 views
2

我想運行一個Devexpress Dxgrid示例程序。 這是here沒有方法「GetValue」的過載需要1個參數

垂直網格代碼:

using DevExpress.Data; 
using DevExpress.Xpf.Editors.Settings; 
using DevExpress.Xpf.Grid; 
using System; 
using System.Collections; 
using System.Collections.ObjectModel; 
using System.Collections.Specialized; 
using System.ComponentModel; 
using System.Linq; 
using System.Reflection; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 

namespace dxExample.VGrid 
{ 
    public partial class VerticalGridControl : GridControl 
    { 
     INotifyCollectionChanged backItemsSourceEvents; 

     object InternalItemsSource 
     { 
      get { return base.ItemsSource; } 
      set { base.ItemsSource = value; } 
     } 
     GridColumnCollection InternalColumns 
     { 
      get { return base.Columns; } 
     } 

     public VerticalRowCollection Rows { get; set; } 
     public bool AutoPopulateRows 
     { 
      get { return (bool)GetValue(AutoPopulateRowsProperty); } 
      set { SetValue(AutoPopulateRowsProperty, value); } 
     } 
     public new object ItemsSource 
     { 
      get { return (object)GetValue(ItemsSourceProperty); } 
      set { SetValue(ItemsSourceProperty, value); } 
     } 

     public VerticalGridControl() 
     { 
      InitializeComponent(); 
      InitializeRowsCollection(); 
      SubscribeItemsSourcePropertyChanged(); 
     } 

     void InitializeRowsCollection() 
     { 
      Rows = new VerticalRowCollection(); 
      Rows.CollectionChanged += OnRowsCollectionChanged; 
     } 
     void UpdateRowsCollection() 
     { 
      if (AutoPopulateRows) 
      { 
       PopulateRows(); 
      } 
     } 
     void SubscribeItemsSourcePropertyChanged() 
     { 
      DependencyPropertyDescriptor itemsSourceDropertyDescriptor = DependencyPropertyDescriptor.FromProperty(VerticalGridControl.ItemsSourceProperty, typeof(VerticalGridControl)); 
      itemsSourceDropertyDescriptor.AddValueChanged(this, new EventHandler(OnItemsSourcePropertyChanged)); 
     } 
     void UpdateInternalColumns() 
     { 
      ICollection itemsSource = (ItemsSource as ICollection); 
      if (itemsSource == null) 
      { 
       Columns.Clear(); 
       return; 
      } 
      Columns.BeginUpdate(); 
      int columnsCount = itemsSource.Count; 
      if (InternalColumns.Count == columnsCount) return; 
      int delta = columnsCount - InternalColumns.Count; 
      if (columnsCount > InternalColumns.Count) 
      { 
       for (int i = InternalColumns.Count; i < columnsCount; i++) 
       { 
        InternalColumns.Add(new GridColumn() { FieldName = i.ToString(), UnboundType = UnboundColumnType.Object }); 
       } 
      } 
      else 
      { 
       for (int i = InternalColumns.Count - 1; i >= columnsCount; i--) 
       { 
        InternalColumns.RemoveAt(i); 
       } 
      } 
      Columns.EndUpdate(); 
     } 
     void UpdateItemsSourceEventsSubscription() 
     { 
      if (backItemsSourceEvents != null) 
      { 
       backItemsSourceEvents.CollectionChanged -= OnItemsSourceCollectionChanged; 
      } 
      if (!(ItemsSource is INotifyCollectionChanged)) return; 
      INotifyCollectionChanged itemsSourceEvents = (ItemsSource as INotifyCollectionChanged); 
      itemsSourceEvents.CollectionChanged += OnItemsSourceCollectionChanged; 
      backItemsSourceEvents = itemsSourceEvents; 
     } 
     void PopulateRows() 
     { 
      IEnumerable itemsSource = (ItemsSource as IEnumerable); 
      if (itemsSource == null) return; 
      IEnumerator itemsSourceEnumerator = itemsSource.GetEnumerator(); 
      itemsSourceEnumerator.MoveNext(); 
      object item = itemsSourceEnumerator.Current; 
      if (item == null) return; 
      PropertyInfo[] itemProps = item.GetType().GetProperties(); 
      for (int i = 0; i < itemProps.Length; i++) 
      { 
       Rows.Add(VerticalRowData.FromPropertyInfo(itemProps[i])); 
      } 
     } 
     void OnRowsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      InternalItemsSource = Rows; 
     } 
     void OnItemsSourcePropertyChanged(object sender, EventArgs e) 
     { 
      UpdateInternalColumns(); 
      UpdateRowsCollection(); 
      UpdateItemsSourceEventsSubscription(); 
     } 
     void OnItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
     { 
      if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Remove) 
      { 
       UpdateInternalColumns(); 
      } 
     } 
     void OnProcessUnboundColumnData(object sender, GridColumnDataEventArgs e) 
     { 
      IList itemsSource = (ItemsSource as IList); 
      if (itemsSource == null) return; 
      VerticalRowData row = Rows[e.ListSourceRowIndex]; 
      object item = itemsSource[Convert.ToInt32(e.Column.FieldName)]; 
      PropertyInfo itemProperty = item.GetType().GetProperty(row.RowName); 
      if (itemProperty == null) return; 
      if (e.IsGetData) 
      { 
       e.Value = itemProperty.GetValue(item); 
      } 
      if (e.IsSetData) 
      { 
       itemProperty.SetValue(item, e.Value); 
      } 
     } 

     public static readonly DependencyProperty AutoPopulateRowsProperty = DependencyProperty.Register("AutoPopulateRows", typeof(bool), typeof(VerticalGridControl), new PropertyMetadata(false)); 
     public static new readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(object), typeof(VerticalGridControl), new PropertyMetadata(null)); 
    } 
    public class BottomIndicatorRowVisibilityConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (values.Count() < 2) 
       return Visibility.Collapsed; 
      if (!((values[0] is int) && (values[1] is int))) 
       return Visibility.Collapsed; 
      return ((int)values[0]) > ((int)values[1]) ? Visibility.Visible : Visibility.Collapsed; 
     } 
     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
    public class DefaultCellTemplateSelector : DataTemplateSelector 
    { 
     public override DataTemplate SelectTemplate(object item, DependencyObject container) 
     { 
      VerticalRowData row = ((item as EditGridCellData).RowData.Row as VerticalRowData); 
      if (row.CellTemplate == null) return base.SelectTemplate(item, container); 
      return row.CellTemplate; 
     } 
    } 
    public class VerticalRowData : DependencyObject 
    { 
     public string RowName { get; set; } 
     public DataTemplate CellTemplate 
     { 
      get { return (DataTemplate)GetValue(CellTemplateProperty); } 
      set { SetValue(CellTemplateProperty, value); } 
     } 

     public static readonly DependencyProperty CellTemplateProperty = DependencyProperty.Register("CellTemplate", typeof(DataTemplate), typeof(VerticalRowData), new PropertyMetadata(null)); 
     public static VerticalRowData FromPropertyInfo(PropertyInfo info) 
     { 
      return new VerticalRowData() { RowName = info.Name }; 
     } 
    } 
    public class VerticalRowCollection : ObservableCollection<VerticalRowData> 
    { 
     protected override void InsertItem(int index, VerticalRowData item) 
     { 
      int existsIndex = IndexOf(item.RowName); 
      if (existsIndex > -1) 
      { 
       if (Items[existsIndex].CellTemplate != null) return; 
       Items[existsIndex].CellTemplate = item.CellTemplate; 
       return; 
      } 
      base.InsertItem(index, item); 
     } 

     int IndexOf(string rowName) 
     { 
      for (int i = 0; i < Items.Count; i++) 
      { 
       if (Items[i].RowName == rowName) return i; 
      } 
       return -1; 
      } 
     } 
    } 

我使用Visual Studio 2010和.NET 4.0, ,我在 「的GetValue」 和 「的SetValue」 獲得唯一的錯誤方法在OnProcessunboundData函數中, 告訴過載運算符分別取「1」和「2」運算符。

它是平臺不匹配的原因。 請嘗試下載示例代碼above並告訴我答案。

感謝, 維韋克

+0

美麗的河畔e他們也需要傳遞一個關鍵值。 –

+0

對於GetValue,有一個名爲item的項, 而該方法在程序中已被重載。 我對此很新,所以我不知道爲什麼它會拋出這個錯誤。 –

回答

3

我一直在尋找通過很多論壇 ,然後我試圖

e.Value = itemProperty.GetValue(item,null); 

和的SetValue: -

itemProperty.SetValue(item, e.Value,null); 

而且它的工作:) 謝謝反正...... :):D

+0

嗨Vivek,我正在使用這個itemProperty.GetValue(currentInstance)的重載版本...我有時會得到這個錯誤。你認爲是什麼原因導致我不使用索引屬性的兩種情況? –

+1

重載PropertyInfo.GetValue(Object)是在.Net Framework 4.5中引入的。在它的封面下,它只是調用this.GetValue(obj,null) – openshac

相關問題