|
| 1 | +using System.Collections.Specialized; |
1 | 2 | using System.Windows.Media;
|
2 | 3 |
|
3 | 4 | namespace MaterialDesignThemes.Wpf;
|
@@ -120,6 +121,147 @@ private static void SetGeneratedTextColumnEditingStyle(object? sender, DataGridA
|
120 | 121 | }
|
121 | 122 | #endregion
|
122 | 123 |
|
| 124 | + #region AttachedProperty : AutoGeneratedComboBoxStyleProperty |
| 125 | + public static readonly DependencyProperty AutoGeneratedComboBoxStyleProperty |
| 126 | + = DependencyProperty.RegisterAttached("AutoGeneratedComboBoxStyle", typeof(Style), typeof(DataGridAssist), |
| 127 | + new PropertyMetadata(default(Style), AutoGeneratedComboBoxStylePropertyChangedCallback)); |
| 128 | + |
| 129 | + public static Style GetAutoGeneratedComboBoxStyle(DataGrid element) |
| 130 | + => (Style)element.GetValue(AutoGeneratedComboBoxStyleProperty); |
| 131 | + public static void SetAutoGeneratedComboBoxStyle(DataGrid element, Style value) |
| 132 | + => element.SetValue(AutoGeneratedComboBoxStyleProperty, value); |
| 133 | + |
| 134 | + private static void AutoGeneratedComboBoxStylePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 135 | + { |
| 136 | + var dataGrid = (DataGrid)d; |
| 137 | + |
| 138 | + dataGrid.AutoGeneratingColumn -= SetGeneratedComboBoxColumnStyle; |
| 139 | + dataGrid.AutoGeneratingColumn += SetGeneratedComboBoxColumnStyle; |
| 140 | + } |
| 141 | + |
| 142 | + private static void SetGeneratedComboBoxColumnStyle(object? sender, DataGridAutoGeneratingColumnEventArgs e) |
| 143 | + { |
| 144 | + if (e.Column is System.Windows.Controls.DataGridComboBoxColumn column && |
| 145 | + sender is DataGrid dataGrid) |
| 146 | + { |
| 147 | + column.ElementStyle = GetAutoGeneratedComboBoxStyle(dataGrid); |
| 148 | + } |
| 149 | + } |
| 150 | + #endregion |
| 151 | + |
| 152 | + #region AttachedProperty : AutoGeneratedEditingComboBoxStyleProperty |
| 153 | + public static readonly DependencyProperty AutoGeneratedEditingComboBoxStyleProperty |
| 154 | + = DependencyProperty.RegisterAttached("AutoGeneratedEditingComboBoxStyle", typeof(Style), typeof(DataGridAssist), |
| 155 | + new PropertyMetadata(default(Style), AutoGeneratedEditingComboBoxStylePropertyChangedCallback)); |
| 156 | + |
| 157 | + public static Style GetAutoGeneratedEditingComboBoxStyle(DataGrid element) |
| 158 | + => (Style)element.GetValue(AutoGeneratedEditingComboBoxStyleProperty); |
| 159 | + public static void SetAutoGeneratedEditingComboBoxStyle(DataGrid element, Style value) |
| 160 | + => element.SetValue(AutoGeneratedEditingComboBoxStyleProperty, value); |
| 161 | + |
| 162 | + private static void AutoGeneratedEditingComboBoxStylePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 163 | + { |
| 164 | + var dataGrid = (DataGrid)d; |
| 165 | + |
| 166 | + dataGrid.AutoGeneratingColumn -= SetGeneratedComboBoxColumnEditingStyle; |
| 167 | + dataGrid.AutoGeneratingColumn += SetGeneratedComboBoxColumnEditingStyle; |
| 168 | + } |
| 169 | + |
| 170 | + private static void SetGeneratedComboBoxColumnEditingStyle(object? sender, DataGridAutoGeneratingColumnEventArgs e) |
| 171 | + { |
| 172 | + if (e.Column is System.Windows.Controls.DataGridComboBoxColumn column && |
| 173 | + sender is DataGrid dataGrid) |
| 174 | + { |
| 175 | + column.EditingElementStyle = GetAutoGeneratedEditingComboBoxStyle(dataGrid); |
| 176 | + } |
| 177 | + } |
| 178 | + #endregion |
| 179 | + |
| 180 | + #region AttachedProperty : ApplyMaterialDesignColumnStylesProperty |
| 181 | + public static readonly DependencyProperty ApplyMaterialDesignColumnStylesProperty |
| 182 | + = DependencyProperty.RegisterAttached("ApplyMaterialDesignColumnStyles", typeof(bool), typeof(DataGridAssist), |
| 183 | + new PropertyMetadata(default(bool), ApplyMaterialDesignColumnStylesPropertyChangedCallback)); |
| 184 | + |
| 185 | + public static void SetApplyMaterialDesignColumnStyles(DataGrid element, bool value) |
| 186 | + => element.SetValue(ApplyMaterialDesignColumnStylesProperty, value); |
| 187 | + |
| 188 | + public static bool GetApplyMaterialDesignColumnStyles(DataGrid element) |
| 189 | + => (bool) element.GetValue(ApplyMaterialDesignColumnStylesProperty); |
| 190 | + |
| 191 | + private static void ApplyMaterialDesignColumnStylesPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 192 | + { |
| 193 | + var dataGrid = (DataGrid)d; |
| 194 | + dataGrid.Columns.CollectionChanged -= ColumnsCollectionChanged; |
| 195 | + if (Equals(true, e.NewValue)) |
| 196 | + { |
| 197 | + dataGrid.Columns.CollectionChanged += ColumnsCollectionChanged; // Auto-generated columns are added later in the chain, thus we subscribe to changes. |
| 198 | + foreach (var column in dataGrid.Columns) |
| 199 | + { |
| 200 | + ApplyMaterialDesignColumnStyleForColumn(dataGrid, column); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + void ColumnsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) |
| 205 | + { |
| 206 | + foreach (DataGridColumn column in e.NewItems?.OfType<DataGridColumn>() ?? Enumerable.Empty<DataGridColumn>()) |
| 207 | + { |
| 208 | + ApplyMaterialDesignColumnStyleForColumn(dataGrid, column); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + private static void ApplyMaterialDesignColumnStyleForColumn(DataGrid dataGrid, DataGridColumn column) |
| 214 | + { |
| 215 | + Style defaultElementStyle = (Style)DataGridBoundColumn.ElementStyleProperty.GetMetadata(column.GetType()).DefaultValue; |
| 216 | + Style defaultEditingElementStyle = (Style)DataGridBoundColumn.EditingElementStyleProperty.GetMetadata(column.GetType()).DefaultValue; |
| 217 | + |
| 218 | + bool applyElementStyle; |
| 219 | + bool applyEditingElementStyle; |
| 220 | + switch (column) |
| 221 | + { |
| 222 | + case DataGridCheckBoxColumn checkBoxColumn: |
| 223 | + applyElementStyle = Equals(checkBoxColumn.ElementStyle, defaultElementStyle); |
| 224 | + applyEditingElementStyle = Equals(checkBoxColumn.EditingElementStyle, defaultEditingElementStyle); |
| 225 | + if (applyElementStyle && GetAutoGeneratedCheckBoxStyle(dataGrid) is { } checkBoxElementStyle) |
| 226 | + { |
| 227 | + checkBoxColumn.ElementStyle = checkBoxElementStyle; |
| 228 | + } |
| 229 | + |
| 230 | + if (applyEditingElementStyle && GetAutoGeneratedEditingCheckBoxStyle(dataGrid) is { } checkBoxEditingElementStyle) |
| 231 | + { |
| 232 | + checkBoxColumn.EditingElementStyle = checkBoxEditingElementStyle; |
| 233 | + } |
| 234 | + break; |
| 235 | + case System.Windows.Controls.DataGridTextColumn textColumn: |
| 236 | + applyElementStyle = Equals(textColumn.ElementStyle, defaultElementStyle); |
| 237 | + applyEditingElementStyle = Equals(textColumn.EditingElementStyle, defaultEditingElementStyle); |
| 238 | + if (applyElementStyle && GetAutoGeneratedTextStyle(dataGrid) is { } textElementStyle) |
| 239 | + { |
| 240 | + textColumn.ElementStyle = textElementStyle; |
| 241 | + } |
| 242 | + |
| 243 | + if (applyEditingElementStyle && GetAutoGeneratedEditingTextStyle(dataGrid) is { } textEditingElementStyle) |
| 244 | + { |
| 245 | + textColumn.EditingElementStyle = textEditingElementStyle; |
| 246 | + } |
| 247 | + break; |
| 248 | + case System.Windows.Controls.DataGridComboBoxColumn comboBoxColumn: |
| 249 | + applyElementStyle = Equals(comboBoxColumn.ElementStyle, defaultElementStyle); |
| 250 | + applyEditingElementStyle = Equals(comboBoxColumn.EditingElementStyle, defaultEditingElementStyle); |
| 251 | + if (applyElementStyle && GetAutoGeneratedComboBoxStyle(dataGrid) is { } comboBoxElementStyle) |
| 252 | + { |
| 253 | + comboBoxColumn.ElementStyle = comboBoxElementStyle; |
| 254 | + } |
| 255 | + |
| 256 | + if (applyEditingElementStyle && GetAutoGeneratedEditingComboBoxStyle(dataGrid) is { } comboBoxEditingElementStyle) |
| 257 | + { |
| 258 | + comboBoxColumn.EditingElementStyle = comboBoxEditingElementStyle; |
| 259 | + } |
| 260 | + break; |
| 261 | + } |
| 262 | + } |
| 263 | + #endregion |
| 264 | + |
123 | 265 | #region AttachedProperty : CellPaddingProperty
|
124 | 266 | public static readonly DependencyProperty CellPaddingProperty
|
125 | 267 | = DependencyProperty.RegisterAttached("CellPadding", typeof(Thickness), typeof(DataGridAssist),
|
|
0 commit comments