Skip to content

Commit cfe9496

Browse files
stepwise-alanpomianowski
andcommittedFeb 1, 2025
Enhance DataGrid UI and Fix Style Issues (#1192)
* fix: Add styles for DataGrid textbox columns * Update DataGrid styles * Remove deprecated color definitions and update styles * Update TextColumnElementStyle to use TextBlock instead of TextBox --------- Co-authored-by: pomian <[email protected]>
1 parent dd33e08 commit cfe9496

File tree

5 files changed

+545
-304
lines changed

5 files changed

+545
-304
lines changed
 

‎src/Wpf.Ui.Gallery/Models/Product.cs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class Product
1515

1616
public string? QuantityPerUnit { get; set; }
1717

18+
public Unit Unit { get; set; }
19+
1820
public double UnitPrice { get; set; }
1921

2022
public string UnitPriceString => UnitPrice.ToString("F2");

‎src/Wpf.Ui.Gallery/Models/Unit.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This Source Code Form is subject to the terms of the MIT License.
2+
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
3+
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
4+
// All Rights Reserved.
5+
6+
namespace Wpf.Ui.Gallery.Models;
7+
8+
public enum Unit
9+
{
10+
Grams,
11+
Kilograms,
12+
Milliliters
13+
}

‎src/Wpf.Ui.Gallery/ViewModels/Pages/Collections/DataGridViewModel.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private static ObservableCollection<Product> GenerateProducts()
2424

2525
var adjectives = new[] { "Red", "Blueberry" };
2626
var names = new[] { "Marmalade", "Dumplings", "Soup" };
27-
/*var units = new[] { "grams", "kilograms", "milliliters" };*/
27+
Unit[] units = [Unit.Grams, Unit.Kilograms, Unit.Milliliters];
2828

2929
for (int i = 0; i < 50; i++)
3030
{
@@ -37,6 +37,7 @@ private static ObservableCollection<Product> GenerateProducts()
3737
adjectives[random.Next(0, adjectives.Length)]
3838
+ " "
3939
+ names[random.Next(0, names.Length)],
40+
Unit = units[random.Next(0, units.Length)],
4041
UnitPrice = Math.Round(random.NextDouble() * 20.0, 3),
4142
UnitsInStock = random.Next(0, 100),
4243
IsVirtual = random.Next(0, 2) == 1,

‎src/Wpf.Ui/Controls/DataGrid/DataGrid.cs

+178-28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace Wpf.Ui.Controls;
1616
/// </summary>
1717
[StyleTypedProperty(Property = nameof(CheckBoxColumnElementStyle), StyleTargetType = typeof(CheckBox))]
1818
[StyleTypedProperty(Property = nameof(CheckBoxColumnEditingElementStyle), StyleTargetType = typeof(CheckBox))]
19+
[StyleTypedProperty(Property = nameof(ComboBoxColumnElementStyle), StyleTargetType = typeof(ComboBox))]
20+
[StyleTypedProperty(Property = nameof(ComboBoxColumnEditingElementStyle), StyleTargetType = typeof(ComboBox))]
21+
[StyleTypedProperty(Property = nameof(TextColumnElementStyle), StyleTargetType = typeof(TextBlock))]
22+
[StyleTypedProperty(Property = nameof(TextColumnEditingElementStyle), StyleTargetType = typeof(TextBox))]
1923
public class DataGrid : System.Windows.Controls.DataGrid
2024
{
2125
/// <summary>Identifies the <see cref="CheckBoxColumnElementStyle"/> dependency property.</summary>
@@ -36,6 +40,42 @@ public class DataGrid : System.Windows.Controls.DataGrid
3640
new FrameworkPropertyMetadata(null)
3741
);
3842

43+
/// <summary>Identifies the <see cref="ComboBoxColumnElementStyle"/> dependency property.</summary>
44+
public static readonly DependencyProperty ComboBoxColumnElementStyleProperty =
45+
DependencyProperty.Register(
46+
nameof(ComboBoxColumnElementStyle),
47+
typeof(Style),
48+
typeof(DataGrid),
49+
new FrameworkPropertyMetadata(null)
50+
);
51+
52+
/// <summary>Identifies the <see cref="ComboBoxColumnEditingElementStyle"/> dependency property.</summary>
53+
public static readonly DependencyProperty ComboBoxColumnEditingElementStyleProperty =
54+
DependencyProperty.Register(
55+
nameof(ComboBoxColumnEditingElementStyle),
56+
typeof(Style),
57+
typeof(DataGrid),
58+
new FrameworkPropertyMetadata(null)
59+
);
60+
61+
/// <summary>Identifies the <see cref="TextColumnElementStyle"/> dependency property.</summary>
62+
public static readonly DependencyProperty TextColumnElementStyleProperty =
63+
DependencyProperty.Register(
64+
nameof(TextColumnElementStyle),
65+
typeof(Style),
66+
typeof(DataGrid),
67+
new FrameworkPropertyMetadata(null)
68+
);
69+
70+
/// <summary>Identifies the <see cref="TextColumnEditingElementStyle"/> dependency property.</summary>
71+
public static readonly DependencyProperty TextColumnEditingElementStyleProperty =
72+
DependencyProperty.Register(
73+
nameof(TextColumnEditingElementStyle),
74+
typeof(Style),
75+
typeof(DataGrid),
76+
new FrameworkPropertyMetadata(null)
77+
);
78+
3979
/// <summary>
4080
/// Gets or sets the style which is applied to all checkbox column in the DataGrid
4181
/// </summary>
@@ -54,6 +94,42 @@ public Style? CheckBoxColumnEditingElementStyle
5494
set => SetValue(CheckBoxColumnEditingElementStyleProperty, value);
5595
}
5696

97+
/// <summary>
98+
/// Gets or sets the style which is applied to all combobox column in the DataGrid
99+
/// </summary>
100+
public Style? ComboBoxColumnElementStyle
101+
{
102+
get => (Style?)GetValue(ComboBoxColumnElementStyleProperty);
103+
set => SetValue(ComboBoxColumnElementStyleProperty, value);
104+
}
105+
106+
/// <summary>
107+
/// Gets or sets the style for all the column comboboxes in the DataGrid
108+
/// </summary>
109+
public Style? ComboBoxColumnEditingElementStyle
110+
{
111+
get => (Style?)GetValue(ComboBoxColumnEditingElementStyleProperty);
112+
set => SetValue(ComboBoxColumnEditingElementStyleProperty, value);
113+
}
114+
115+
/// <summary>
116+
/// Gets or sets the style which is applied to all textbox column in the DataGrid
117+
/// </summary>
118+
public Style? TextColumnElementStyle
119+
{
120+
get => (Style?)GetValue(TextColumnElementStyleProperty);
121+
set => SetValue(TextColumnElementStyleProperty, value);
122+
}
123+
124+
/// <summary>
125+
/// Gets or sets the style for all the column textboxes in the DataGrid
126+
/// </summary>
127+
public Style? TextColumnEditingElementStyle
128+
{
129+
get => (Style?)GetValue(TextColumnEditingElementStyleProperty);
130+
set => SetValue(TextColumnEditingElementStyleProperty, value);
131+
}
132+
57133
protected override void OnInitialized(EventArgs e)
58134
{
59135
Columns.CollectionChanged += ColumnsOnCollectionChanged;
@@ -78,35 +154,109 @@ private void UpdateColumnElementStyles()
78154

79155
private void UpdateSingleColumn(DataGridColumn dataGridColumn)
80156
{
81-
if (dataGridColumn is DataGridCheckBoxColumn checkBoxColumn)
157+
switch (dataGridColumn)
82158
{
83-
if (
84-
checkBoxColumn.ReadLocalValue(DataGridCheckBoxColumn.ElementStyleProperty)
85-
== DependencyProperty.UnsetValue
86-
)
87-
{
88-
_ = BindingOperations.SetBinding(
89-
checkBoxColumn,
90-
DataGridCheckBoxColumn.ElementStyleProperty,
91-
new Binding { Path = new PropertyPath(CheckBoxColumnElementStyleProperty), Source = this }
92-
);
93-
}
94-
95-
if (
96-
checkBoxColumn.ReadLocalValue(DataGridCheckBoxColumn.EditingElementStyleProperty)
97-
== DependencyProperty.UnsetValue
98-
)
99-
{
100-
_ = BindingOperations.SetBinding(
101-
checkBoxColumn,
102-
DataGridCheckBoxColumn.EditingElementStyleProperty,
103-
new Binding
104-
{
105-
Path = new PropertyPath(CheckBoxColumnEditingElementStyleProperty),
106-
Source = this,
107-
}
108-
);
109-
}
159+
case DataGridCheckBoxColumn checkBoxColumn:
160+
if (
161+
checkBoxColumn.ReadLocalValue(DataGridBoundColumn.ElementStyleProperty)
162+
== DependencyProperty.UnsetValue
163+
)
164+
{
165+
_ = BindingOperations.SetBinding(
166+
checkBoxColumn,
167+
DataGridBoundColumn.ElementStyleProperty,
168+
new Binding { Path = new PropertyPath(CheckBoxColumnElementStyleProperty), Source = this }
169+
);
170+
}
171+
172+
if (
173+
checkBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
174+
== DependencyProperty.UnsetValue
175+
)
176+
{
177+
_ = BindingOperations.SetBinding(
178+
checkBoxColumn,
179+
DataGridBoundColumn.EditingElementStyleProperty,
180+
new Binding
181+
{
182+
Path = new PropertyPath(CheckBoxColumnEditingElementStyleProperty), Source = this
183+
}
184+
);
185+
}
186+
187+
break;
188+
189+
case DataGridComboBoxColumn comboBoxColumn:
190+
if (
191+
comboBoxColumn.ReadLocalValue(DataGridBoundColumn.ElementStyleProperty)
192+
== DependencyProperty.UnsetValue
193+
)
194+
{
195+
_ = BindingOperations.SetBinding(
196+
comboBoxColumn,
197+
DataGridBoundColumn.ElementStyleProperty,
198+
new Binding { Path = new PropertyPath(ComboBoxColumnElementStyleProperty), Source = this }
199+
);
200+
}
201+
202+
if (
203+
comboBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
204+
== DependencyProperty.UnsetValue
205+
)
206+
{
207+
_ = BindingOperations.SetBinding(
208+
comboBoxColumn,
209+
DataGridBoundColumn.EditingElementStyleProperty,
210+
new Binding
211+
{
212+
Path = new PropertyPath(ComboBoxColumnEditingElementStyleProperty), Source = this
213+
}
214+
);
215+
}
216+
217+
if (
218+
comboBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
219+
== DependencyProperty.UnsetValue
220+
)
221+
{
222+
_ = BindingOperations.SetBinding(
223+
comboBoxColumn,
224+
DataGridBoundColumn.EditingElementStyleProperty,
225+
new Binding
226+
{
227+
Path = new PropertyPath(ComboBoxColumnEditingElementStyleProperty), Source = this
228+
}
229+
);
230+
}
231+
232+
break;
233+
234+
case DataGridTextColumn textBoxColumn:
235+
if (
236+
textBoxColumn.ReadLocalValue(DataGridBoundColumn.ElementStyleProperty)
237+
== DependencyProperty.UnsetValue
238+
)
239+
{
240+
_ = BindingOperations.SetBinding(
241+
textBoxColumn,
242+
DataGridBoundColumn.ElementStyleProperty,
243+
new Binding { Path = new PropertyPath(TextColumnElementStyleProperty), Source = this }
244+
);
245+
}
246+
247+
if (
248+
textBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
249+
== DependencyProperty.UnsetValue
250+
)
251+
{
252+
_ = BindingOperations.SetBinding(
253+
textBoxColumn,
254+
DataGridBoundColumn.EditingElementStyleProperty,
255+
new Binding { Path = new PropertyPath(TextColumnEditingElementStyleProperty), Source = this }
256+
);
257+
}
258+
259+
break;
110260
}
111261
}
112262
}

‎src/Wpf.Ui/Controls/DataGrid/DataGrid.xaml

+350-275
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.