Windows Presentation Foundation

From Richard's Wiki
Revision as of 23:48, 15 November 2011 by Rkdrm (Talk | contribs)

Jump to: navigation, search
  • Set max characters in column for WPF DataGridColumn. Use DataGridTextColumn.EditingElementStyle.Setters.Add(new Setter(TextBox.MaxLengthProperty, maxColumnLength)):
private void DataGrid_AutoGeneratedColumns(object sender, EventArgs e)
       {
           DataGrid datagrid = sender as DataGrid;
           if (datagrid == null) return;
           var columns = datagrid.Columns;
           var orderedColumns = columns
               .OrderBy(c => GetDisplayInfo(ViewModel.CurrentType, c.Header as string).DisplayOrder)
               .ToList();
           foreach (DataGridColumn dataGridColumn in columns)
           {
               dataGridColumn.DisplayIndex = orderedColumns.IndexOf(dataGridColumn);
                   DisplayInfo displayInfo = GetDisplayInfo(ViewModel.CurrentType, dataGridColumn.Header as string);
               dataGridColumn.Header = displayInfo.DisplayName;
               if ((dataGridColumn is DataGridTextColumn) &&
                   (displayInfo.MaxLength.HasValue))
               {
                   int maxColumnLength = displayInfo.MaxLength.Value;
                   ((DataGridTextColumn)dataGridColumn).EditingElementStyle.Setters.Add(new Setter(TextBox.MaxLengthProperty, maxColumnLength));
               }
           }
       }