1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Text; using System.Threading.Tasks; using Microsoft.VisualBasic; using Newtonsoft.Json; using System.Windows.Controls.Primitives; using System.Windows.Controls; using System.Windows; using System.Windows.Input; using System.ComponentModel; namespace APS.Synergy.Client.Framework.Infrastructure.DataGridExtender { public class DataGridExtender : DataGrid { public DataGridExtender() { this.ColumnReordered += DataGridControl_ColumnReordered; //Cannot use layoutupdated for saving as it will save before datagrid is loaded //this.LayoutUpdated += DataGridControl_LayoutUpdated; //this.AutoGeneratedColumns += DataGridControl_AutoGenerateColumns; this.Loaded += DataGridControl_Loaded; } private void DataGridControl_Loaded(object sender, RoutedEventArgs e) { var lcolumnList = ReadColumnsFromDisk(); try { if (lcolumnList != null && lcolumnList.Count > 0) { foreach (var column in lcolumnList) { //var litem = lcolumnList.FirstOrDefault(x => x.Header == GetColumnIdentifier(column)); var litem = this.Columns.FirstOrDefault(x => GetColumnIdentifier(x).Equals(column.Header)); if (column.ColumnWidth > 0) { litem.Width = new DataGridLength(column.ColumnWidth, DataGridLengthUnitType.Pixel); } if (litem != null) { //find display index and put it at the end. Let datagridcolum sort do its thing and itll push up all the way to missing columndisplayindex //Need to do it this way otherwise if the displayindex exists in another item it does weird stuff this.Columns.FirstOrDefault(x => x.DisplayIndex == litem.DisplayIndex).DisplayIndex = this.Columns.Count - 1; } } } //Column resize event doesnt exist so need to set one PropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(DataGridColumn.ActualWidthProperty, typeof(DataGridColumn)); //add event to each column foreach (DataGridColumn column in this.Columns) { //Add a listener for this column's width pd.AddValueChanged(column, new EventHandler(ColumnWidthPropertyChanged)); } } catch (Exception ex) { Debug.WriteLine(String.Format("DATAGRIDEXTENDER: [Loaded] Error reloading the column layout for grid {0}: {1}", (String.IsNullOrWhiteSpace(this.ControlName) ? this.ControlName : this.DataContext.ToString()), ex.GetBaseException().Message)); Debug.WriteLine(ex.ToString()); } } public static readonly DependencyProperty ControlNameProperty = DependencyProperty.Register("ControlName", typeof(string), typeof(DataGridExtender)); public string ControlName { get { return System.Convert.ToString(GetValue(ControlNameProperty)); } set { SetValue(ControlNameProperty, value); } } private void DataGridControl_Unloaded(object sender, RoutedEventArgs e) { SaveColumnLayout(); } private void DataGridControl_AutoGenerateColumns(object sender, EventArgs e) { var lcolumnList = ReadColumnsFromDisk(); try { if (lcolumnList != null && lcolumnList.Count > 0) { foreach (var column in this.Columns) { var litem = lcolumnList.FirstOrDefault(x => x.Header.Replace(" ", "") == GetColumnIdentifier(column).Replace(" ", "")); if (litem != null) { column.DisplayIndex = litem.DisplayIndex; column.Width = (litem.ColumnWidth > 0) ? new DataGridLength(litem.ColumnWidth) : Double.NaN; } } } } catch (Exception ex) { Debug.WriteLine(String.Format("DATAGRIDEXTENDER: [AutoGenerateColumns] Error reloading the column layout for grid {0}: {1}", (String.IsNullOrWhiteSpace(this.ControlName) ? this.ControlName : this.DataContext.ToString()), ex.GetBaseException().Message)); Debug.WriteLine(ex.ToString()); } } private string GetColumnIdentifier(DataGridColumn column) { // If the column has am x:Name set, then use that is the // identifer, otherwise try the header, otherwise make something up if (column is DataGridTextColumnExtender && !String.IsNullOrWhiteSpace(((DataGridTextColumnExtender)column).ColumnName)) { return ((DataGridTextColumnExtender)column).ColumnName; } if (column is DataGridTemplateColumnExtender && !String.IsNullOrWhiteSpace(((DataGridTemplateColumnExtender)column).ColumnName)) { return ((DataGridTemplateColumnExtender)column).ColumnName; } if (column is DataGridCheckBoxColumnExtender && !String.IsNullOrWhiteSpace(((DataGridCheckBoxColumnExtender)column).ColumnName)) { return ((DataGridCheckBoxColumnExtender)column).ColumnName; } if (column is DataGridComboBoxColumnExtender && !String.IsNullOrWhiteSpace(((DataGridComboBoxColumnExtender)column).ColumnName)) { return ((DataGridComboBoxColumnExtender)column).ColumnName; } if (column is DataGridHyperlinkColumnExtender && !String.IsNullOrWhiteSpace(((DataGridHyperlinkColumnExtender)column).ColumnName)) { return ((DataGridHyperlinkColumnExtender)column).ColumnName; } if (column.Header != null && !String.IsNullOrWhiteSpace(column.Header.ToString())) { return column.Header.ToString(); } return "column_" + this.Columns.IndexOf(column); // Everything has failed so we need to try and use something else } private void DataGridControl_ColumnReordered(object sender, DataGridColumnEventArgs e) { SaveColumnLayout(); } private void DataGridControl_LayoutUpdated(object sender, EventArgs e) { SaveColumnLayout(); } private void SaveColumnLayout() { List<ColumnItem> lColumnList = new List<ColumnItem>(); ColumnItem lColumnItem; foreach (var column in this.Columns) { lColumnItem = new ColumnItem() { DisplayIndex = column.DisplayIndex, Header = GetColumnIdentifier(column), ColumnWidth = column.Width.DesiredValue }; lColumnList.Add(lColumnItem); } lColumnList = lColumnList.OrderBy(x => x.DisplayIndex).ToList(); WriteColumnsToDisk(lColumnList); } private void WriteColumnsToDisk(List<ColumnItem> columnList) { string file = string.Empty; try { file = GetDataGridFileName(); var path = System.IO.Path.GetDirectoryName(file); if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); var contents = Newtonsoft.Json.JsonConvert.SerializeObject(columnList); System.IO.File.WriteAllText(GetDataGridFileName(), contents); } catch (Exception ex) { Debug.WriteLine("Failed to save datagrid columns to {0} : {1}", file, ex.GetBaseException().Message); Debug.WriteLine(ex.ToString()); } } private List<ColumnItem> ReadColumnsFromDisk() { string file = string.Empty; try { file = GetDataGridFileName(); if (System.IO.File.Exists(file)) { var contents = System.IO.File.ReadAllText(file); if (!string.IsNullOrWhiteSpace(contents)) { List<ColumnItem> columnList; columnList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ColumnItem>>(contents); return columnList; } } } catch (Exception ex) { Debug.WriteLine("Failed to read datagrid columns to {0} : {1}", file, ex.GetBaseException().Message); Debug.WriteLine(ex.ToString()); } return null; } private string GetDataGridFileName() { if (String.IsNullOrEmpty(this.ControlName)) { return System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "APS", "Synergy3", "Client", "Settings", this.DataContext.ToString() + "_datagrid.json"); } return System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "APS", "Synergy3", "Client", "Settings", this.ControlName + "_datagrid.json"); } //column width change for mouse click stuff private bool _columnWidthChanging; private void ColumnWidthPropertyChanged(object sender, EventArgs e) { // listen for when the mouse is released _columnWidthChanging = true; if (sender != null) Mouse.AddPreviewMouseUpHandler(this, BaseDataGrid_MouseLeftButtonUp); } void BaseDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (_columnWidthChanging) { _columnWidthChanging = false; SaveColumnLayout(); } } } public class ColumnItem { public int DisplayIndex { get; set; } public string Header { get; set; } public double ColumnWidth { get; set; } } public class DataGridTextColumnExtender : DataGridTextColumn { public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.Register("ColumnName", typeof(string), typeof(DataGridTextColumnExtender)); public string ColumnName { get { return System.Convert.ToString(GetValue(ColumnNameProperty)); } set { SetValue(ColumnNameProperty, value); } } } public class DataGridTemplateColumnExtender : DataGridTemplateColumn { public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.Register("ColumnName", typeof(string), typeof(DataGridTemplateColumnExtender)); public string ColumnName { get { return System.Convert.ToString(GetValue(ColumnNameProperty)); } set { SetValue(ColumnNameProperty, value); } } } public class DataGridComboBoxColumnExtender : DataGridComboBoxColumn { public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.Register("ColumnName", typeof(string), typeof(DataGridComboBoxColumnExtender)); public string ColumnName { get { return System.Convert.ToString(GetValue(ColumnNameProperty)); } set { SetValue(ColumnNameProperty, value); } } } public class DataGridCheckBoxColumnExtender : DataGridCheckBoxColumn { public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.Register("ColumnName", typeof(string), typeof(DataGridCheckBoxColumnExtender)); public string ColumnName { get { return System.Convert.ToString(GetValue(ColumnNameProperty)); } set { SetValue(ColumnNameProperty, value); } } } public class DataGridHyperlinkColumnExtender : DataGridHyperlinkColumn { public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.Register("ColumnName", typeof(string), typeof(DataGridHyperlinkColumnExtender)); public string ColumnName { get { return System.Convert.ToString(GetValue(ColumnNameProperty)); } set { SetValue(ColumnNameProperty, value); } } } } |