Silverlight DataGrid异常重新排序列标题

时间:2008-11-26 17:01:54

标签: silverlight datagrid

我正在尝试通过更改列标题DisplayIndex值来设置silverlight数据网格中列标题的初始显示顺序。如果我尝试在页面加载时设置列顺序,则会出现超出范围的异常。如果我在稍后的时间设置列顺序(相同的例程),例如,在按钮单击处理程序中,它可以工作。这只是silverlight数据网格控件中的一个错误吗?关于可能解决的建议?

3 个答案:

答案 0 :(得分:1)

我猜你从Page Loaded事件修改DataGrid中列的DisplayIndex时遇到问题,因为此时尚未创建它们。您没有说,但我假设您正在使用DataGrid来自动生成列,否则您只需在定义DataGrid列时在XAML中设置DisplayIndex。

当DataGrid生成Columns时,它会为每个Column触发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;
    }
}

答案 1 :(得分: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因为到目前为止还没有加载控件

答案 2 :(得分: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;
    }

}