Skip to content

Commit

Permalink
(GH-3840) Introduce attached properties for DataGridComboBoxColumn
Browse files Browse the repository at this point in the history
- Add AutoGeneratedComboBoxColumnStyle and AutoGeneratedComboBoxColumnEditingStyle attached properties
- Add new style MahApps.Styles.ComboBox.DataGrid
  • Loading branch information
punker76 committed Jun 8, 2020
1 parent d359edf commit 00cad35
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MetroDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="clr-namespace:MetroDemo.Models"
xmlns:vc="clr-namespace:MetroDemo.ValueConverter"
d:DataContext="{d:DesignInstance local:MainWindowViewModel}"
d:DesignHeight="600"
Expand Down Expand Up @@ -196,10 +197,12 @@
EditingElementStyle="{DynamicResource MahApps.Styles.CheckBox.DataGrid.Win10}"
ElementStyle="{DynamicResource MahApps.Styles.CheckBox.DataGrid.Win10}"
Header="Album Selected" />
<DataGridTextColumn Binding="{Binding Title}"
EditingElementStyle="{StaticResource MahApps.Styles.TextBox.DataGrid.Editing.Custom}"
Header="Title" />
<DataGridTextColumn Binding="{Binding Title}" Header="Title" />
<DataGridTextColumn Binding="{Binding Artist.Name}" Header="Artist" />
<DataGridComboBoxColumn DisplayMemberPath="Name"
Header="Artist"
ItemsSource="{x:Static models:SampleData.Artists}"
SelectedItemBinding="{Binding Artist}" />
<DataGridTextColumn Binding="{Binding Genre.Name}" Header="Genre" />
<controls:DataGridNumericUpDownColumn Binding="{Binding Price}"
Header="Price"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ protected virtual void BindColumnStyles(DataGridColumn column)
SetBinding(checkBoxColumn, DataGridBoundColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedCheckBoxColumnEditingStyleProperty);
break;
case DataGridComboBoxColumn comboBoxColumn:
// SetBinding(comboBoxColumn, DataGridComboBoxColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnStyleProperty);
// SetBinding(comboBoxColumn, DataGridComboBoxColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnEditingStyleProperty);
SetBinding(comboBoxColumn, DataGridComboBoxColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnStyleProperty);
SetBinding(comboBoxColumn, DataGridComboBoxColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnEditingStyleProperty);
break;
case DataGridHyperlinkColumn hyperlinkColumn:
// SetBinding(hyperlinkColumn, DataGridHyperlinkColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedHyperlinkColumnStyleProperty);
Expand All @@ -108,8 +108,8 @@ protected virtual void ClearColumnStyles(DataGridColumn column)
ClearBinding(checkBoxColumn, DataGridBoundColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedCheckBoxColumnEditingStyleProperty);
break;
case DataGridComboBoxColumn comboBoxColumn:
// ClearBinding(comboBoxColumn, DataGridComboBoxColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnStyleProperty);
// ClearBinding(comboBoxColumn, DataGridComboBoxColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnEditingStyleProperty);
ClearBinding(comboBoxColumn, DataGridComboBoxColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnStyleProperty);
ClearBinding(comboBoxColumn, DataGridComboBoxColumn.EditingElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedComboBoxColumnEditingStyleProperty);
break;
case DataGridHyperlinkColumn hyperlinkColumn:
// ClearBinding(hyperlinkColumn, DataGridHyperlinkColumn.ElementStyleProperty, this.dataGrid, DataGridHelper.AutoGeneratedHyperlinkColumnStyleProperty);
Expand Down
146 changes: 138 additions & 8 deletions src/MahApps.Metro/Controls/Helper/DataGridHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ public static class DataGridHelper
{
private static DataGrid _suppressComboAutoDropDown;

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

/// <summary>
/// Gets the ElementStyle for the autogenerated DataGridCheckBoxColumn.
/// Helper for getting <see cref="AutoGeneratedCheckBoxColumnStyleProperty"/> from <paramref name="element"/>.
///
/// If a style is set, the DataGridCheckBoxColumn of the DataGrid will use this style for normal cells.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to read <see cref="AutoGeneratedCheckBoxColumnStyleProperty"/> from.</param>
/// <returns>AutoGeneratedCheckBoxColumnStyle property value.</returns>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static Style GetAutoGeneratedCheckBoxColumnStyle(UIElement element)
Expand All @@ -30,24 +39,37 @@ public static Style GetAutoGeneratedCheckBoxColumnStyle(UIElement element)
}

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

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

/// <summary>
/// Gets the EditingElementStyle for the autogenerated DataGridCheckBoxColumn.
/// Helper for getting <see cref="AutoGeneratedCheckBoxColumnEditingStyleProperty"/> from <paramref name="element"/>.
///
/// If a style is set, the DataGridCheckBoxColumn of the DataGrid will use this style for cells in edit mode.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to read <see cref="AutoGeneratedCheckBoxColumnEditingStyleProperty"/> from.</param>
/// <returns>AutoGeneratedCheckBoxColumnEditingStyle property value.</returns>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static Style GetAutoGeneratedCheckBoxColumnEditingStyle(UIElement element)
Expand All @@ -56,24 +78,37 @@ public static Style GetAutoGeneratedCheckBoxColumnEditingStyle(UIElement element
}

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

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

/// <summary>
/// Gets the ElementStyle for the autogenerated DataGridTextColumn.
/// Helper for getting <see cref="AutoGeneratedTextColumnStyleProperty"/> from <paramref name="element"/>.
///
/// If a style is set, the DataGridTextColumn columns of the DataGrid will use this style for normal cells.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to read <see cref="AutoGeneratedTextColumnStyleProperty"/> from.</param>
/// <returns>AutoGeneratedTextColumnStyle property value.</returns>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static Style GetAutoGeneratedTextColumnStyle(UIElement element)
Expand All @@ -82,24 +117,37 @@ public static Style GetAutoGeneratedTextColumnStyle(UIElement element)
}

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

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

/// <summary>
/// Gets the EditingElementStyle for the autogenerated DataGridTextColumn.
/// Helper for getting <see cref="AutoGeneratedTextColumnEditingStyleProperty"/> from <paramref name="element"/>.
///
/// If a style is set, the DataGridTextColumn of the DataGrid will use this style for cells in edit mode.
/// </summary>
/// <param name="element"><see cref="UIElement"/> to read <see cref="AutoGeneratedTextColumnEditingStyleProperty"/> from.</param>
/// <returns>AutoGeneratedTextColumnEditingStyle property value.</returns>
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(DataGrid))]
public static Style GetAutoGeneratedTextColumnEditingStyle(UIElement element)
Expand All @@ -108,15 +156,97 @@ public static Style GetAutoGeneratedTextColumnEditingStyle(UIElement element)
}

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

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

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

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

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

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

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

public static readonly DependencyProperty CellPaddingProperty
= DependencyProperty.RegisterAttached("CellPadding",
typeof(Thickness),
Expand Down
Loading

0 comments on commit 00cad35

Please sign in to comment.