我想通過更改列標題DisplayIndex值來設置Silverlight數據網格中列標題的初始顯示順序。如果我嘗試在頁面加載時設置列順序,則會出現超出範圍的異常。如果我稍後設置列順序(相同的例程),就像在按鈕點擊處理程序中一樣,它可以工作。這只是Silverlight數據網格控件中的一個錯誤?建議可能的解決方法?Silverlight DataGrid異常重新排序列標題
1
A
回答
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
其實你需要訂閱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;
}
}
相關問題
- 1. Silverlight 4 datagrid不重新排序
- 2. Datagrid中的列重新排序
- 3. Silverlight 4 Datagrid排序
- 4. Silverlight的DataGrid標題結合常數
- 5. WPF4 Datagrid不在列標題上排序
- 6. WPF DataGrid:重新排列行?
- 7. 排序/重新排序Primefaces Datagrid
- 8. DataGrid排序/更新問題
- 9. 序列化異常WCF + Silverlight
- 10. Silverlight Datagrid數字排序
- 11. Silverlight datagrid重新綁定整個列
- 12. Silverlight Datagrid列標題文本分隔線
- 13. Silverlight 4的DataGrid列標題樣式
- 14. 的Silverlight 4的DataGrid中心列標題
- 15. 在Silverlight中綁定DataGrid列標題
- 16. Silverlight DataGrid控件自定義列標題
- 17. Silverlight DataGrid重繪問題
- 18. 重新排序SlickGrid列重置列標題HTML
- 19. 如何重新排列DataGrid的列?
- 20. 標題樣式datagrid silverlight
- 21. silverlight 3 Datagrid標題定製
- 22. 刷新Silverlight DataGrid的問題
- 23. DataGrid標題排序方向圖標
- 24. WPF DataGrid的自定義排序:重新排列物品的ItemsSource
- 25. JQGrid雖然列重新排序 - 問題與標題顯示
- 26. SAS:PROC TABULATE重新排列標題子組
- 27. 重新應用排序到DataGrid?
- 28. Silverlight Datagrid刷新
- 29. 重新排序列
- 30. 如何在重新排序datagrid的列之後獲取新的列順序?
在裝情況下,任何人仍然在意,是的:這個問題也會出現AutoGenerateColumns == false和no,在這種情況下,你不會得到任何AutoGeneratingColumn事件。不,Loaded事件也不起作用。在我的情況下,我通過處理LayoutUpdated事件並僅在我的ItemsSource長度> 0時更新顯示索引來解決。祝您好運。 – 2015-02-09 10:47:39