private static DataGridColumn CreateColumn(DataGrid gridView, object columnSource) { DataGridTextColumn column = new DataGridTextColumn(); var info = columnSource as DataGridColumnInfoM; if (!string.IsNullOrEmpty(info.Header)) { column.Header = info.Header; } if(!string.IsNullOrEmpty(info.Binding)) { column.Binding = new Binding(info.Binding); } if(!string.IsNullOrEmpty(info.HorizontalContentAlignment)) { //column.ElementStyle = new Binding(info.HorizontalContentAlignment); Style right = new Style(typeof(TextBlock)); Setter setRight = new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right); right.Setters.Add(setRight); setRight = new Setter(TextBlock.MarginProperty, new Thickness(5,0,5,0)); right.Setters.Add(setRight); column.ElementStyle = right; } if (info.Width > 0) { column.Width = info.Width; } return column; #if false DataTemplate template = new DataTemplate(); FrameworkElementFactory factory = new FrameworkElementFactory(typeof(TextBlock)); factory.SetValue(TextBlock.HorizontalAlignmentProperty, info.HorizontalAlignment); if (info.Foreground != null) { factory.SetValue(TextBlock.ForegroundProperty, info.Foreground); } if (info.ForegroundTriggerParameters != null && !string.IsNullOrEmpty(info.ForegroundMemberPath)) { foreach (var o in info.ForegroundTriggerParameters) { var trigger = new DataTrigger(); trigger.Binding = new Binding(info.ForegroundMemberPath); trigger.Value = o.Key; var setter = new Setter(); setter.Property = TextBlock.ForegroundProperty; setter.Value = o.Value; trigger.Setters.Add(setter); template.Triggers.Add(trigger); } } var binding = new Binding(info.DisplayMemberPath); if (!string.IsNullOrEmpty(info.StringFormat)) { binding.StringFormat = string.Format("{{0:{0}}}", info.StringFormat); } if (info.Converter != null) { binding.Converter = info.Converter; binding.ConverterParameter = info.ConverterParameter; } factory.SetBinding(TextBlock.TextProperty, binding); template.VisualTree = factory; column.CellTemplate = template; ListViewItemSortData scd = new ListViewItemSortData(info.DisplayMemberPath); scd.Direction = info.SortDirection; if (SortColumns.Count(c => c.Key == column) == 0) { SortColumns.Add(column, scd); } else { Console.WriteLine("Repeat SortColumns.................."); } return column; #endif }