Skip to content

Commit

Permalink
(GH-3840) Introduce attached properties for DataGridNumericUpDownColumn
Browse files Browse the repository at this point in the history
- Add AutoGeneratedNumericUpDownColumnStyle and AutoGeneratedNumericUpDownColumnEditingStyle attached properties
- Use real default styles (without looking for a base style)
  • Loading branch information
punker76 committed Jun 8, 2020
1 parent 877f1cd commit ec3aec1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/MahApps.Metro/Controls/DataGridNumericUpDownColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ public static Style DefaultEditingElementStyle
{
if (_defaultEditingElementStyle == null)
{
var numericUpDown = new NumericUpDown();

var style = numericUpDown.TryFindResource("MahApps.Styles.NumericUpDown.DataGridColumnEditing") is Style baseStyle ? new Style(typeof(NumericUpDown), baseStyle) : new Style(typeof(NumericUpDown));
var style = new Style(typeof(NumericUpDown));

style.Setters.Add(new Setter(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled));
style.Setters.Add(new Setter(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled));
style.Setters.Add(new Setter(ControlsHelper.DisabledVisualElementVisibilityProperty, Visibility.Collapsed));

style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0)));
style.Setters.Add(new Setter(NumericUpDown.HideUpDownButtonsProperty, false));
style.Setters.Add(new Setter(FrameworkElement.MinHeightProperty, 0d));
style.Setters.Add(new Setter(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Top));
style.Setters.Add(new Setter(Control.VerticalContentAlignmentProperty, VerticalAlignment.Center));
style.Setters.Add(new Setter(ControlsHelper.CornerRadiusProperty, new CornerRadius(0)));

style.Seal();
_defaultEditingElementStyle = style;
}
Expand All @@ -48,9 +53,7 @@ public static Style DefaultElementStyle
{
if (_defaultElementStyle == null)
{
var numericUpDown = new NumericUpDown();

var style = numericUpDown.TryFindResource("MahApps.Styles.NumericUpDown.DataGridColumn") is Style baseStyle ? new Style(typeof(NumericUpDown), baseStyle) : new Style(typeof(NumericUpDown));
var style = new Style(typeof(NumericUpDown));

style.Setters.Add(new Setter(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled));
style.Setters.Add(new Setter(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled));
Expand All @@ -59,6 +62,14 @@ public static Style DefaultElementStyle
style.Setters.Add(new Setter(UIElement.IsHitTestVisibleProperty, false));
style.Setters.Add(new Setter(UIElement.FocusableProperty, false));

style.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Transparent));
style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0)));
style.Setters.Add(new Setter(NumericUpDown.HideUpDownButtonsProperty, true));
style.Setters.Add(new Setter(FrameworkElement.MinHeightProperty, 0d));
style.Setters.Add(new Setter(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Top));
style.Setters.Add(new Setter(Control.VerticalContentAlignmentProperty, VerticalAlignment.Center));
style.Setters.Add(new Setter(ControlsHelper.CornerRadiusProperty, new CornerRadius(0)));

style.Seal();
_defaultElementStyle = style;
}
Expand Down Expand Up @@ -100,11 +111,7 @@ protected override FrameworkElement GenerateElement(DataGridCell cell, object da

private NumericUpDown GenerateNumericUpDown(bool isEditing, DataGridCell cell)
{
NumericUpDown numericUpDown = (cell != null) ? (cell.Content as NumericUpDown) : null;
if (numericUpDown == null)
{
numericUpDown = new NumericUpDown();
}
var numericUpDown = cell?.Content as NumericUpDown ?? new NumericUpDown();

SyncColumnProperty(this, numericUpDown, FontFamilyProperty, TextElement.FontFamilyProperty);
SyncColumnProperty(this, numericUpDown, FontSizeProperty, TextElement.FontSizeProperty);
Expand Down Expand Up @@ -145,9 +152,8 @@ private NumericUpDown GenerateNumericUpDown(bool isEditing, DataGridCell cell)
this.ApplyStyle(isEditing, true, numericUpDown);
ApplyBinding(this.Binding, numericUpDown, NumericUpDown.ValueProperty);

numericUpDown.InterceptArrowKeys = true;
numericUpDown.InterceptMouseWheel = true;
numericUpDown.Speedup = true;
numericUpDown.Focusable = isEditing;
numericUpDown.IsHitTestVisible = isEditing;

return numericUpDown;
}
Expand All @@ -160,8 +166,7 @@ private NumericUpDown GenerateNumericUpDown(bool isEditing, DataGridCell cell)
/// <returns>The unedited value of the cell.</returns>
protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
{
NumericUpDown numericUpDown = editingElement as NumericUpDown;
if (numericUpDown != null)
if (editingElement is NumericUpDown numericUpDown)
{
numericUpDown.Focus();
numericUpDown.SelectAll();
Expand Down Expand Up @@ -237,7 +242,6 @@ public CultureInfo Culture
set { this.SetValue(CultureProperty, value); }
}


public static readonly DependencyProperty TextAlignmentProperty =
NumericUpDown.TextAlignmentProperty.AddOwner(
typeof(DataGridNumericUpDownColumn),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ protected virtual void BindColumnStyles(DataGridColumn column)
SetBinding(comboBoxColumn, DataGridComboBoxColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnStyleProperty);
SetBinding(comboBoxColumn, DataGridComboBoxColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnEditingStyleProperty);
break;
case DataGridNumericUpDownColumn numericUpDownColumn:
SetBinding(numericUpDownColumn, DataGridBoundColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedNumericUpDownColumnStyleProperty);
SetBinding(numericUpDownColumn, DataGridBoundColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedNumericUpDownColumnEditingStyleProperty);
break;
case DataGridHyperlinkColumn hyperlinkColumn:
// SetBinding(hyperlinkColumn, DataGridHyperlinkColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedHyperlinkColumnStyleProperty);
// SetBinding(hyperlinkColumn, DataGridHyperlinkColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedHyperlinkColumnEditingStyleProperty);
Expand All @@ -111,6 +115,10 @@ protected virtual void ClearColumnStyles(DataGridColumn column)
ClearBinding(comboBoxColumn, DataGridComboBoxColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnStyleProperty);
ClearBinding(comboBoxColumn, DataGridComboBoxColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnEditingStyleProperty);
break;
case DataGridNumericUpDownColumn numericUpDownColumn:
ClearBinding(numericUpDownColumn, DataGridBoundColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedNumericUpDownColumnStyleProperty);
ClearBinding(numericUpDownColumn, DataGridBoundColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedNumericUpDownColumnEditingStyleProperty);
break;
case DataGridHyperlinkColumn hyperlinkColumn:
// ClearBinding(hyperlinkColumn, DataGridHyperlinkColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedHyperlinkColumnStyleProperty);
// ClearBinding(hyperlinkColumn, DataGridHyperlinkColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedHyperlinkColumnEditingStyleProperty);
Expand Down
78 changes: 78 additions & 0 deletions src/MahApps.Metro/Controls/Helper/DataGridHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,84 @@ public static void SetAutoGeneratedComboBoxColumnEditingStyle(UIElement element,
element.SetValue(AutoGeneratedComboBoxColumnEditingStyleProperty, value);
}

/// <summary>
/// The DependencyProperty for the <see cref="DataGrid"/>' AutoGeneratedNumericUpDownColumnStyle property.
///
/// If a style is set, the DataGridNumericUpDownColumn columns of the DataGrid will use this style for normal cells.
/// </summary>
public static readonly DependencyProperty AutoGeneratedNumericUpDownColumnStyleProperty
= DependencyProperty.RegisterAttached("AutoGeneratedNumericUpDownColumnStyle",
typeof(Style),
typeof(DataGridHelper),
new PropertyMetadata(default(Style)));

/// <summary>
/// Helper for getting <see cref="AutoGeneratedNumericUpDownColumnStyleProperty"/> from <paramref name="element"/>.
///
/// If a style is set, the DataGridNumericUpDownColumn columns of the DataGrid will use this style for normal cells.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to read <see cref="AutoGeneratedNumericUpDownColumnStyleProperty"/> from.</param>
/// <returns>AutoGeneratedNumericUpDownColumnStyle property value.</returns>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static Style GetAutoGeneratedNumericUpDownColumnStyle(UIElement element)
{
return (Style)element.GetValue(AutoGeneratedNumericUpDownColumnStyleProperty);
}

/// <summary>
/// Helper for setting <see cref="AutoGeneratedNumericUpDownColumnStyleProperty"/> on <paramref name="element"/>.
///
/// If a style is set, the DataGridNumericUpDownColumn columns of the DataGrid will use this style for normal cells.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to set <see cref="AutoGeneratedNumericUpDownColumnStyleProperty"/> on.</param>
/// <param name="value">AutoGeneratedNumericUpDownColumnStyle property value.</param>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static void SetAutoGeneratedNumericUpDownColumnStyle(UIElement element, Style value)
{
element.SetValue(AutoGeneratedNumericUpDownColumnStyleProperty, value);
}

/// <summary>
/// The DependencyProperty for the <see cref="DataGrid"/>' AutoGeneratedNumericUpDownColumnEditingStyle property.
///
/// If a style is set, the DataGridNumericUpDownColumn of the DataGrid will use this style for cells in edit mode.
/// </summary>
public static readonly DependencyProperty AutoGeneratedNumericUpDownColumnEditingStyleProperty
= DependencyProperty.RegisterAttached("AutoGeneratedNumericUpDownColumnEditingStyle",
typeof(Style),
typeof(DataGridHelper),
new PropertyMetadata(default(Style)));

/// <summary>
/// Helper for getting <see cref="AutoGeneratedNumericUpDownColumnEditingStyleProperty"/> from <paramref name="element"/>.
///
/// If a style is set, the DataGridNumericUpDownColumn of the DataGrid will use this style for cells in edit mode.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to read <see cref="AutoGeneratedNumericUpDownColumnEditingStyleProperty"/> from.</param>
/// <returns>AutoGeneratedNumericUpDownColumnEditingStyle property value.</returns>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static Style GetAutoGeneratedNumericUpDownColumnEditingStyle(UIElement element)
{
return (Style)element.GetValue(AutoGeneratedNumericUpDownColumnEditingStyleProperty);
}

/// <summary>
/// Helper for setting <see cref="AutoGeneratedNumericUpDownColumnEditingStyleProperty"/> on <paramref name="element"/>.
///
/// If a style is set, the DataGridNumericUpDownColumn of the DataGrid will use this style for cells in edit mode.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to set <see cref="AutoGeneratedNumericUpDownColumnEditingStyleProperty"/> on.</param>
/// <param name="value">AutoGeneratedNumericUpDownColumnEditingStyle property value.</param>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static void SetAutoGeneratedNumericUpDownColumnEditingStyle(UIElement element, Style value)
{
element.SetValue(AutoGeneratedNumericUpDownColumnEditingStyleProperty, value);
}

public static readonly DependencyProperty CellPaddingProperty
= DependencyProperty.RegisterAttached("CellPadding",
typeof(Thickness),
Expand Down
5 changes: 5 additions & 0 deletions src/MahApps.Metro/Styles/Controls.DataGrid.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="HideUpDownButtons" Value="True" />
<Setter Property="InterceptArrowKeys" Value="True" />
<Setter Property="InterceptMouseWheel" Value="True" />
<Setter Property="MinHeight" Value="0" />
<Setter Property="Speedup" Value="True" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="mah:ControlsHelper.CornerRadius" Value="0" />
Expand Down Expand Up @@ -735,6 +738,8 @@
<Setter Property="mah:DataGridHelper.AutoGeneratedCheckBoxColumnStyle" Value="{StaticResource MahApps.Styles.CheckBox.DataGrid}" />
<Setter Property="mah:DataGridHelper.AutoGeneratedComboBoxColumnEditingStyle" Value="{StaticResource MahApps.Styles.ComboBox.DataGrid.Editing}" />
<Setter Property="mah:DataGridHelper.AutoGeneratedComboBoxColumnStyle" Value="{StaticResource MahApps.Styles.ComboBox.DataGrid}" />
<Setter Property="mah:DataGridHelper.AutoGeneratedNumericUpDownColumnEditingStyle" Value="{StaticResource MahApps.Styles.NumericUpDown.DataGridColumnEditing}" />
<Setter Property="mah:DataGridHelper.AutoGeneratedNumericUpDownColumnStyle" Value="{StaticResource MahApps.Styles.NumericUpDown.DataGridColumn}" />
<Setter Property="mah:DataGridHelper.AutoGeneratedTextColumnEditingStyle" Value="{StaticResource MahApps.Styles.TextBox.DataGrid.Editing}" />
<Setter Property="mah:DataGridHelper.AutoGeneratedTextColumnStyle" Value="{StaticResource MahApps.Styles.TextBlock.DataGrid}" />
<Setter Property="mah:DataGridHelper.ColumnStylesHelper" Value="{mah:DataGridColumnStylesHelper}" />
Expand Down

0 comments on commit ec3aec1

Please sign in to comment.