Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance DataGrid UI and Fix Style Issues #1192

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Wpf.Ui.Gallery/Models/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Product

public string? QuantityPerUnit { get; set; }

public Unit Unit { get; set; }

public double UnitPrice { get; set; }

public string UnitPriceString => UnitPrice.ToString("F2");
Expand Down
13 changes: 13 additions & 0 deletions src/Wpf.Ui.Gallery/Models/Unit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

namespace Wpf.Ui.Gallery.Models;

public enum Unit
{
Grams,
Kilograms,
Milliliters
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static ObservableCollection<Product> GenerateProducts()

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

for (int i = 0; i < 50; i++)
{
Expand All @@ -32,6 +32,7 @@ private static ObservableCollection<Product> GenerateProducts()
adjectives[random.Next(0, adjectives.Length)]
+ " "
+ names[random.Next(0, names.Length)],
Unit = units[random.Next(0, units.Length)],
UnitPrice = Math.Round(random.NextDouble() * 20.0, 3),
UnitsInStock = random.Next(0, 100),
IsVirtual = random.Next(0, 2) == 1
Expand Down
206 changes: 178 additions & 28 deletions src/Wpf.Ui/Controls/DataGrid/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace Wpf.Ui.Controls;
/// </summary>
[StyleTypedProperty(Property = nameof(CheckBoxColumnElementStyle), StyleTargetType = typeof(CheckBox))]
[StyleTypedProperty(Property = nameof(CheckBoxColumnEditingElementStyle), StyleTargetType = typeof(CheckBox))]
[StyleTypedProperty(Property = nameof(ComboBoxColumnElementStyle), StyleTargetType = typeof(ComboBox))]
[StyleTypedProperty(Property = nameof(ComboBoxColumnEditingElementStyle), StyleTargetType = typeof(ComboBox))]
[StyleTypedProperty(Property = nameof(TextColumnElementStyle), StyleTargetType = typeof(TextBlock))]
[StyleTypedProperty(Property = nameof(TextColumnEditingElementStyle), StyleTargetType = typeof(TextBox))]
public class DataGrid : System.Windows.Controls.DataGrid
{
/// <summary>Identifies the <see cref="CheckBoxColumnElementStyle"/> dependency property.</summary>
Expand All @@ -36,6 +40,42 @@ public class DataGrid : System.Windows.Controls.DataGrid
new FrameworkPropertyMetadata(null)
);

/// <summary>Identifies the <see cref="ComboBoxColumnElementStyle"/> dependency property.</summary>
public static readonly DependencyProperty ComboBoxColumnElementStyleProperty =
DependencyProperty.Register(
nameof(ComboBoxColumnElementStyle),
typeof(Style),
typeof(DataGrid),
new FrameworkPropertyMetadata(null)
);

/// <summary>Identifies the <see cref="ComboBoxColumnEditingElementStyle"/> dependency property.</summary>
public static readonly DependencyProperty ComboBoxColumnEditingElementStyleProperty =
DependencyProperty.Register(
nameof(ComboBoxColumnEditingElementStyle),
typeof(Style),
typeof(DataGrid),
new FrameworkPropertyMetadata(null)
);

/// <summary>Identifies the <see cref="TextColumnElementStyle"/> dependency property.</summary>
public static readonly DependencyProperty TextColumnElementStyleProperty =
DependencyProperty.Register(
nameof(TextColumnElementStyle),
typeof(Style),
typeof(DataGrid),
new FrameworkPropertyMetadata(null)
);

/// <summary>Identifies the <see cref="TextColumnEditingElementStyle"/> dependency property.</summary>
public static readonly DependencyProperty TextColumnEditingElementStyleProperty =
DependencyProperty.Register(
nameof(TextColumnEditingElementStyle),
typeof(Style),
typeof(DataGrid),
new FrameworkPropertyMetadata(null)
);

/// <summary>
/// Gets or sets the style which is applied to all checkbox column in the DataGrid
/// </summary>
Expand All @@ -54,6 +94,42 @@ public Style? CheckBoxColumnEditingElementStyle
set => SetValue(CheckBoxColumnEditingElementStyleProperty, value);
}

/// <summary>
/// Gets or sets the style which is applied to all combobox column in the DataGrid
/// </summary>
public Style? ComboBoxColumnElementStyle
{
get => (Style?)GetValue(ComboBoxColumnElementStyleProperty);
set => SetValue(ComboBoxColumnElementStyleProperty, value);
}

/// <summary>
/// Gets or sets the style for all the column comboboxes in the DataGrid
/// </summary>
public Style? ComboBoxColumnEditingElementStyle
{
get => (Style?)GetValue(ComboBoxColumnEditingElementStyleProperty);
set => SetValue(ComboBoxColumnEditingElementStyleProperty, value);
}

/// <summary>
/// Gets or sets the style which is applied to all textbox column in the DataGrid
/// </summary>
public Style? TextColumnElementStyle
{
get => (Style?)GetValue(TextColumnElementStyleProperty);
set => SetValue(TextColumnElementStyleProperty, value);
}

/// <summary>
/// Gets or sets the style for all the column textboxes in the DataGrid
/// </summary>
public Style? TextColumnEditingElementStyle
{
get => (Style?)GetValue(TextColumnEditingElementStyleProperty);
set => SetValue(TextColumnEditingElementStyleProperty, value);
}

protected override void OnInitialized(EventArgs e)
{
Columns.CollectionChanged += ColumnsOnCollectionChanged;
Expand All @@ -78,35 +154,109 @@ private void UpdateColumnElementStyles()

private void UpdateSingleColumn(DataGridColumn dataGridColumn)
{
if (dataGridColumn is DataGridCheckBoxColumn checkBoxColumn)
switch (dataGridColumn)
{
if (
checkBoxColumn.ReadLocalValue(DataGridCheckBoxColumn.ElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
checkBoxColumn,
DataGridCheckBoxColumn.ElementStyleProperty,
new Binding { Path = new PropertyPath(CheckBoxColumnElementStyleProperty), Source = this }
);
}

if (
checkBoxColumn.ReadLocalValue(DataGridCheckBoxColumn.EditingElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
checkBoxColumn,
DataGridCheckBoxColumn.EditingElementStyleProperty,
new Binding
{
Path = new PropertyPath(CheckBoxColumnEditingElementStyleProperty),
Source = this
}
);
}
case DataGridCheckBoxColumn checkBoxColumn:
if (
checkBoxColumn.ReadLocalValue(DataGridBoundColumn.ElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
checkBoxColumn,
DataGridBoundColumn.ElementStyleProperty,
new Binding { Path = new PropertyPath(CheckBoxColumnElementStyleProperty), Source = this }
);
}

if (
checkBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
checkBoxColumn,
DataGridBoundColumn.EditingElementStyleProperty,
new Binding
{
Path = new PropertyPath(CheckBoxColumnEditingElementStyleProperty), Source = this
}
);
}

break;

case DataGridComboBoxColumn comboBoxColumn:
if (
comboBoxColumn.ReadLocalValue(DataGridBoundColumn.ElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
comboBoxColumn,
DataGridBoundColumn.ElementStyleProperty,
new Binding { Path = new PropertyPath(ComboBoxColumnElementStyleProperty), Source = this }
);
}

if (
comboBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
comboBoxColumn,
DataGridBoundColumn.EditingElementStyleProperty,
new Binding
{
Path = new PropertyPath(ComboBoxColumnEditingElementStyleProperty), Source = this
}
);
}

if (
comboBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
comboBoxColumn,
DataGridBoundColumn.EditingElementStyleProperty,
new Binding
{
Path = new PropertyPath(ComboBoxColumnEditingElementStyleProperty), Source = this
}
);
}

break;

case DataGridTextColumn textBoxColumn:
if (
textBoxColumn.ReadLocalValue(DataGridBoundColumn.ElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
textBoxColumn,
DataGridBoundColumn.ElementStyleProperty,
new Binding { Path = new PropertyPath(TextColumnElementStyleProperty), Source = this }
);
}

if (
textBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty)
== DependencyProperty.UnsetValue
)
{
_ = BindingOperations.SetBinding(
textBoxColumn,
DataGridBoundColumn.EditingElementStyleProperty,
new Binding { Path = new PropertyPath(TextColumnEditingElementStyleProperty), Source = this }
);
}

break;
}
}
}
Loading
Loading