2008-11-26 60 views
1

我想通過更改列標題DisplayIndex值來設置Silverlight數據網格中列標題的初始顯示順序。如果我嘗試在頁面加載時設置列順序,則會出現超出範圍的異常。如果我稍後設置列順序(相同的例程),就像在按鈕點擊處理程序中一樣,它可以工作。這只是Silverlight數據網格控件中的一個錯誤?建議可能的解決方法?Silverlight DataGrid異常重新排序列標題

回答

1

我猜你從頁面加載事件中修改DataGrid中列的DisplayIndex時出現問題,因爲它們尚未在此時創建。你不會說,但我假設你得到的DataGrid AutoGenerate你的列,否則你可以在定義DataGrid列時在你的XAML中設置DisplayIndex。

當DataGrid生成列時,它爲每個列引發一個AutoGeneratingColumn事件,允許您修改其屬性。這有點粗糙,但一種解決方案可能是使用爲其創建列的PropertyName在此事件的處理程序中設置DisplayIndex。

private void grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{ 
    switch (e.PropertyName) 
    { 
     case "Name": 
      e.Column.DisplayIndex = 1; 
      break; 

     case "Age": 
      e.Column.DisplayIndex = 0; 
      break; 
    } 
} 
+0

在裝情況下,任何人仍然在意,是的:這個問題也會出現AutoGenerateColumns == false和no,在這種情況下,你不會得到任何AutoGeneratingColumn事件。不,Loaded事件也不起作用。在我的情況下,我通過處理LayoutUpdated事件並僅在我的ItemsSource長度> 0時更新顯示索引來解決。祝您好運。 – 2015-02-09 10:47:39

0

其實你需要訂閱grid.Loaded事件和重排colums有:

public UserManagementControl() 
    { 
     InitializeComponent(); 
     dataGridUsers.Loaded += new RoutedEventHandler(dataGridUsers_Loaded); 
    } 

    void dataGridUsers_Loaded(object sender, RoutedEventArgs e) 
    { 
     dataGridUsers.Columns[0].DisplayIndex = 1; 
    } 

你有ArgumentOutOfRangeException因爲控制尚未到目前爲止

0
/// <summary> 
/// Automation DataGrid Control - Columns Localization and Ordering 
/// Option1: Localization of Columns Automatically 
/// Option2: Ordering columns in DataGrid Automatically 
/// </summary> 
/// <param name="dataGrid"> DataGrid control</param> 
/// <param name="ContractType"> Contract of Row DataItem 
/// Example: typeof(ClientType) 
/// </param> 
/// <param name="columns"> Ordered Properties of Contract 
/// Example: columns = "Id_Client,Client,GeographyItem,Flag_Approved,ClientType,ClientRelation,ClientPrestigeLevel" 
/// </param> 
public void AutomateDataGridColumns(DataGrid dataGrid, Type Contract, String columns) 
{ 
    try 
    { 
     List<String> OrderedColumns = columns.Split(new string[] { ",", "|", ";" }, StringSplitOptions.RemoveEmptyEntries).ToList(); 

     //Buid Order of created COLUMNS 
     dataGrid.Loaded += (sndr, arg) => 
     { 
      if (dataGrid.Columns.Count == OrderedColumns.Count && dataGrid.AutoGenerateColumns == true)      
      { 
       foreach (var item in dataGrid.Columns) 
       { 
        Int32 displayIndex = OrderedColumns.IndexOf(item.Header.ToString()); 
        if (displayIndex != -1) 
        { item.DisplayIndex = displayIndex; }       
       } 
      }; 
     }; 

     //DataGridColumn Localization 
     dataGrid.AutoGeneratingColumn += (sndr, arg) => 
     { 
       LocalizeDataGridColumn(sndr as DataGrid, arg, Contract, OrderedColumns); 

       //We need To Update DataGrid after last Column Localized -->so Loaded event will be Raised/ 
       // or ArgumentOutOfRange Exception will be thrown 
       if (dataGrid.Columns.Count == OrderedColumns.Count && dataGrid.AutoGenerateColumns == true) 
       { 
        dataGrid.UpdateLayout(); 
       } 
     }; 

    }   
    catch (Exception exc) 
    { throw exc; 
    } 
} 



/// <summary> 
/// DataGridColumn Control Localization 
/// </summary> 
/// <param name="dataGrid">Host DataGrid control </param> 
/// <param name="arg">Auto Generated Column arg </param> 
/// <param name="Contract">Type Contract</param> 
/// <param name="localizationColumns">Ordered Properties to Contract </param> 
protected void LocalizeDataGridColumn(DataGrid dataGrid, DataGridAutoGeneratingColumnEventArgs arg, Type Contract, List<String> localizationColumns) 
{ 
    try 
    { 
     DataGridColumn Column = arg.Column; 

     if (localizationColumns.Contains(Column.Header.ToString())) 
     { 
      // LOCALIZING Column.Header      

      // Check column local resource key exist 
      // CultureKeys - local Culture enum type 
      // SystemDispatcher - is My SL4 MEF Bootstrappper 
      // LocalizationService - is My Localization service in SL4 
      // if somebody is interested i can share more of my LocalizationService - use mail  
      CultureKeys currntCulture = SystemDispatcher.LocalizationService.CurrentCulture; 
      string ResourceKey = LocalResKeys.BoPropElmNameLoc.ToString() + "\\" + Contract.Name + @"|" + Column.Header.ToString(); 

      if (SystemDispatcher.LocalizationService.CultureResources[currntCulture].Item2.ContainsKey(ResourceKey)) 
       Column.Header = SystemDispatcher.LocalizationService.CultureResources[currntCulture].Item2[ResourceKey];          
     } 
     else 
     { arg.Cancel = true; 
     } 
    } 
    catch (Exception exc) 
    { 
     throw exc; 
    } 

}