From adf9bde7066d3bebbe1b56a9ae64f6cb8697ccb0 Mon Sep 17 00:00:00 2001 From: Shuolin Wang Date: Thu, 8 Aug 2024 10:53:30 +0800 Subject: [PATCH 1/4] fix: Add styles for DataGrid textbox columns --- src/Wpf.Ui.Gallery/Models/Product.cs | 2 + src/Wpf.Ui.Gallery/Models/Unit.cs | 13 ++ .../Pages/Collections/DataGridViewModel.cs | 3 +- src/Wpf.Ui/Controls/DataGrid/DataGrid.cs | 123 ++++++++++++++---- 4 files changed, 112 insertions(+), 29 deletions(-) create mode 100644 src/Wpf.Ui.Gallery/Models/Unit.cs diff --git a/src/Wpf.Ui.Gallery/Models/Product.cs b/src/Wpf.Ui.Gallery/Models/Product.cs index 2f76bc433..b574c6a02 100644 --- a/src/Wpf.Ui.Gallery/Models/Product.cs +++ b/src/Wpf.Ui.Gallery/Models/Product.cs @@ -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"); diff --git a/src/Wpf.Ui.Gallery/Models/Unit.cs b/src/Wpf.Ui.Gallery/Models/Unit.cs new file mode 100644 index 000000000..6144ab902 --- /dev/null +++ b/src/Wpf.Ui.Gallery/Models/Unit.cs @@ -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 +} diff --git a/src/Wpf.Ui.Gallery/ViewModels/Pages/Collections/DataGridViewModel.cs b/src/Wpf.Ui.Gallery/ViewModels/Pages/Collections/DataGridViewModel.cs index cef593de8..fa30d78bc 100644 --- a/src/Wpf.Ui.Gallery/ViewModels/Pages/Collections/DataGridViewModel.cs +++ b/src/Wpf.Ui.Gallery/ViewModels/Pages/Collections/DataGridViewModel.cs @@ -19,7 +19,7 @@ private static ObservableCollection 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++) { @@ -32,6 +32,7 @@ private static ObservableCollection 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 diff --git a/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs b/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs index dfbd4196f..11590efb1 100644 --- a/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs +++ b/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs @@ -16,6 +16,8 @@ namespace Wpf.Ui.Controls; /// [StyleTypedProperty(Property = nameof(CheckBoxColumnElementStyle), StyleTargetType = typeof(CheckBox))] [StyleTypedProperty(Property = nameof(CheckBoxColumnEditingElementStyle), StyleTargetType = typeof(CheckBox))] +[StyleTypedProperty(Property = nameof(TextBoxColumnElementStyle), StyleTargetType = typeof(TextBox))] +[StyleTypedProperty(Property = nameof(TextBoxColumnEditingElementStyle), StyleTargetType = typeof(TextBox))] public class DataGrid : System.Windows.Controls.DataGrid { /// Identifies the dependency property. @@ -36,6 +38,24 @@ public class DataGrid : System.Windows.Controls.DataGrid new FrameworkPropertyMetadata(null) ); + /// Identifies the dependency property. + public static readonly DependencyProperty TextBoxColumnElementStyleProperty = + DependencyProperty.Register( + nameof(TextBoxColumnElementStyle), + typeof(Style), + typeof(DataGrid), + new FrameworkPropertyMetadata(null) + ); + + /// Identifies the dependency property. + public static readonly DependencyProperty TextBoxColumnEditingElementStyleProperty = + DependencyProperty.Register( + nameof(TextBoxColumnEditingElementStyle), + typeof(Style), + typeof(DataGrid), + new FrameworkPropertyMetadata(null) + ); + /// /// Gets or sets the style which is applied to all checkbox column in the DataGrid /// @@ -54,6 +74,24 @@ public Style? CheckBoxColumnEditingElementStyle set => SetValue(CheckBoxColumnEditingElementStyleProperty, value); } + /// + /// Gets or sets the style which is applied to all textbox column in the DataGrid + /// + public Style? TextBoxColumnElementStyle + { + get => (Style?)GetValue(TextBoxColumnElementStyleProperty); + set => SetValue(TextBoxColumnElementStyleProperty, value); + } + + /// + /// Gets or sets the style for all the column textboxes in the DataGrid + /// + public Style? TextBoxColumnEditingElementStyle + { + get => (Style?)GetValue(TextBoxColumnEditingElementStyleProperty); + set => SetValue(TextBoxColumnEditingElementStyleProperty, value); + } + protected override void OnInitialized(EventArgs e) { Columns.CollectionChanged += ColumnsOnCollectionChanged; @@ -78,35 +116,64 @@ 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 DataGridTextColumn textBoxColumn: + if ( + textBoxColumn.ReadLocalValue(DataGridBoundColumn.ElementStyleProperty) + == DependencyProperty.UnsetValue + ) + { + _ = BindingOperations.SetBinding( + textBoxColumn, + DataGridBoundColumn.ElementStyleProperty, + new Binding { Path = new PropertyPath(TextBoxColumnElementStyleProperty), Source = this } + ); + } + + if ( + textBoxColumn.ReadLocalValue(DataGridBoundColumn.EditingElementStyleProperty) + == DependencyProperty.UnsetValue + ) + { + _ = BindingOperations.SetBinding( + textBoxColumn, + DataGridBoundColumn.EditingElementStyleProperty, + new Binding { Path = new PropertyPath(TextBoxColumnEditingElementStyleProperty), Source = this } + ); + } + + break; } } } From 9af949744336cd7a4b4c4388313a2f07aa2ab16d Mon Sep 17 00:00:00 2001 From: Shuolin Wang Date: Tue, 13 Aug 2024 08:58:22 +0800 Subject: [PATCH 2/4] Update DataGrid styles --- src/Wpf.Ui/Controls/DataGrid/DataGrid.cs | 115 ++++- src/Wpf.Ui/Controls/DataGrid/DataGrid.xaml | 543 +++++++++++++-------- 2 files changed, 443 insertions(+), 215 deletions(-) diff --git a/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs b/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs index 11590efb1..e7cde516b 100644 --- a/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs +++ b/src/Wpf.Ui/Controls/DataGrid/DataGrid.cs @@ -16,8 +16,10 @@ namespace Wpf.Ui.Controls; /// [StyleTypedProperty(Property = nameof(CheckBoxColumnElementStyle), StyleTargetType = typeof(CheckBox))] [StyleTypedProperty(Property = nameof(CheckBoxColumnEditingElementStyle), StyleTargetType = typeof(CheckBox))] -[StyleTypedProperty(Property = nameof(TextBoxColumnElementStyle), StyleTargetType = typeof(TextBox))] -[StyleTypedProperty(Property = nameof(TextBoxColumnEditingElementStyle), StyleTargetType = typeof(TextBox))] +[StyleTypedProperty(Property = nameof(ComboBoxColumnElementStyle), StyleTargetType = typeof(ComboBox))] +[StyleTypedProperty(Property = nameof(ComboBoxColumnEditingElementStyle), StyleTargetType = typeof(ComboBox))] +[StyleTypedProperty(Property = nameof(TextColumnElementStyle), StyleTargetType = typeof(TextBox))] +[StyleTypedProperty(Property = nameof(TextColumnEditingElementStyle), StyleTargetType = typeof(TextBox))] public class DataGrid : System.Windows.Controls.DataGrid { /// Identifies the dependency property. @@ -38,19 +40,37 @@ public class DataGrid : System.Windows.Controls.DataGrid new FrameworkPropertyMetadata(null) ); - /// Identifies the dependency property. - public static readonly DependencyProperty TextBoxColumnElementStyleProperty = + /// Identifies the dependency property. + public static readonly DependencyProperty ComboBoxColumnElementStyleProperty = DependencyProperty.Register( - nameof(TextBoxColumnElementStyle), + nameof(ComboBoxColumnElementStyle), typeof(Style), typeof(DataGrid), new FrameworkPropertyMetadata(null) ); - /// Identifies the dependency property. - public static readonly DependencyProperty TextBoxColumnEditingElementStyleProperty = + /// Identifies the dependency property. + public static readonly DependencyProperty ComboBoxColumnEditingElementStyleProperty = DependencyProperty.Register( - nameof(TextBoxColumnEditingElementStyle), + nameof(ComboBoxColumnEditingElementStyle), + typeof(Style), + typeof(DataGrid), + new FrameworkPropertyMetadata(null) + ); + + /// Identifies the dependency property. + public static readonly DependencyProperty TextColumnElementStyleProperty = + DependencyProperty.Register( + nameof(TextColumnElementStyle), + typeof(Style), + typeof(DataGrid), + new FrameworkPropertyMetadata(null) + ); + + /// Identifies the dependency property. + public static readonly DependencyProperty TextColumnEditingElementStyleProperty = + DependencyProperty.Register( + nameof(TextColumnEditingElementStyle), typeof(Style), typeof(DataGrid), new FrameworkPropertyMetadata(null) @@ -74,22 +94,40 @@ public Style? CheckBoxColumnEditingElementStyle set => SetValue(CheckBoxColumnEditingElementStyleProperty, value); } + /// + /// Gets or sets the style which is applied to all combobox column in the DataGrid + /// + public Style? ComboBoxColumnElementStyle + { + get => (Style?)GetValue(ComboBoxColumnElementStyleProperty); + set => SetValue(ComboBoxColumnElementStyleProperty, value); + } + + /// + /// Gets or sets the style for all the column comboboxes in the DataGrid + /// + public Style? ComboBoxColumnEditingElementStyle + { + get => (Style?)GetValue(ComboBoxColumnEditingElementStyleProperty); + set => SetValue(ComboBoxColumnEditingElementStyleProperty, value); + } + /// /// Gets or sets the style which is applied to all textbox column in the DataGrid /// - public Style? TextBoxColumnElementStyle + public Style? TextColumnElementStyle { - get => (Style?)GetValue(TextBoxColumnElementStyleProperty); - set => SetValue(TextBoxColumnElementStyleProperty, value); + get => (Style?)GetValue(TextColumnElementStyleProperty); + set => SetValue(TextColumnElementStyleProperty, value); } /// /// Gets or sets the style for all the column textboxes in the DataGrid /// - public Style? TextBoxColumnEditingElementStyle + public Style? TextColumnEditingElementStyle { - get => (Style?)GetValue(TextBoxColumnEditingElementStyleProperty); - set => SetValue(TextBoxColumnEditingElementStyleProperty, value); + get => (Style?)GetValue(TextColumnEditingElementStyleProperty); + set => SetValue(TextColumnEditingElementStyleProperty, value); } protected override void OnInitialized(EventArgs e) @@ -148,6 +186,51 @@ private void UpdateSingleColumn(DataGridColumn dataGridColumn) 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) @@ -157,7 +240,7 @@ private void UpdateSingleColumn(DataGridColumn dataGridColumn) _ = BindingOperations.SetBinding( textBoxColumn, DataGridBoundColumn.ElementStyleProperty, - new Binding { Path = new PropertyPath(TextBoxColumnElementStyleProperty), Source = this } + new Binding { Path = new PropertyPath(TextColumnElementStyleProperty), Source = this } ); } @@ -169,7 +252,7 @@ private void UpdateSingleColumn(DataGridColumn dataGridColumn) _ = BindingOperations.SetBinding( textBoxColumn, DataGridBoundColumn.EditingElementStyleProperty, - new Binding { Path = new PropertyPath(TextBoxColumnEditingElementStyleProperty), Source = this } + new Binding { Path = new PropertyPath(TextColumnEditingElementStyleProperty), Source = this } ); } diff --git a/src/Wpf.Ui/Controls/DataGrid/DataGrid.xaml b/src/Wpf.Ui/Controls/DataGrid/DataGrid.xaml index 46a301bbc..fff5b3b21 100644 --- a/src/Wpf.Ui/Controls/DataGrid/DataGrid.xaml +++ b/src/Wpf.Ui/Controls/DataGrid/DataGrid.xaml @@ -22,6 +22,14 @@ xmlns:converters="clr-namespace:Wpf.Ui.Converters" xmlns:system="clr-namespace:System;assembly=System.Runtime"> + + + + + + + + @@ -64,61 +72,61 @@ - + + + + + + + + + + - - - - - - - #FFFF0000 - - 11,5,11,6 - 1 - 8,0,0,0 - 14 - 22 - 22 -