Browse Source

Merge pull request #8568 from AvaloniaUI/datagrid-control-theme

Add DataGrid fluent Control Theme
composition/license
Max Katz 4 years ago
committed by GitHub
parent
commit
497bc8a23b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      samples/ControlCatalog/Pages/DataGridPage.xaml
  2. 10
      src/Avalonia.Controls.DataGrid/DataGridCell.cs
  3. 20
      src/Avalonia.Controls.DataGrid/DataGridColumn.cs
  4. 1
      src/Avalonia.Controls.DataGrid/DataGridRowGroupHeader.cs
  5. 28
      src/Avalonia.Controls.DataGrid/DataGridTextColumn.cs
  6. 1078
      src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml

9
samples/ControlCatalog/Pages/DataGridPage.xaml

@ -10,11 +10,11 @@
<TextBlock Text="{Binding}"/> <TextBlock Text="{Binding}"/>
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
<ControlTheme x:Key="GdpCell" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Background" Value="{Binding Path=GDP, Mode=OneWay, Converter={StaticResource GDPConverter}}" />
</ControlTheme>
</UserControl.Resources> </UserControl.Resources>
<UserControl.Styles> <UserControl.Styles>
<Style Selector="DataGridCell.gdp">
<Setter Property="Background" Value="{Binding Path=GDP, Mode=OneWay, Converter={StaticResource GDPConverter}}" />
</Style>
<Style Selector="DataGridColumnHeader:nth-last-child(1)"> <Style Selector="DataGridColumnHeader:nth-last-child(1)">
<Setter Property="FontWeight" Value="Bold" /> <Setter Property="FontWeight" Value="Bold" />
</Style> </Style>
@ -54,7 +54,8 @@
<DataGridTextColumn Header="Region" Binding="{CompiledBinding Region}" Width="4*" x:DataType="local:Country" /> <DataGridTextColumn Header="Region" Binding="{CompiledBinding Region}" Width="4*" x:DataType="local:Country" />
<DataGridTextColumn Header="Population" Binding="{Binding Population}" Width="3*" /> <DataGridTextColumn Header="Population" Binding="{Binding Population}" Width="3*" />
<DataGridTextColumn Header="Area" Binding="{Binding Area}" Width="3*" /> <DataGridTextColumn Header="Area" Binding="{Binding Area}" Width="3*" />
<DataGridTextColumn Header="GDP" Binding="{Binding GDP}" Width="3*" CellStyleClasses="gdp" <DataGridTextColumn Header="GDP" Binding="{Binding GDP}" Width="3*"
CellTheme="{StaticResource GdpCell}"
MinWidth="200" MinWidth="200"
IsVisible="{Binding #ShowGDP.IsChecked}"/> IsVisible="{Binding #ShowGDP.IsChecked}"/>
</DataGrid.Columns> </DataGrid.Columns>

10
src/Avalonia.Controls.DataGrid/DataGridCell.cs

@ -14,7 +14,7 @@ namespace Avalonia.Controls
/// Represents an individual <see cref="T:Avalonia.Controls.DataGrid" /> cell. /// Represents an individual <see cref="T:Avalonia.Controls.DataGrid" /> cell.
/// </summary> /// </summary>
[TemplatePart(DATAGRIDCELL_elementRightGridLine, typeof(Rectangle))] [TemplatePart(DATAGRIDCELL_elementRightGridLine, typeof(Rectangle))]
[PseudoClasses(":selected", ":current", ":edited", ":invalid")] [PseudoClasses(":selected", ":current", ":edited", ":invalid", ":focus")]
public class DataGridCell : ContentControl public class DataGridCell : ContentControl
{ {
private const string DATAGRIDCELL_elementRightGridLine = "PART_RightGridLine"; private const string DATAGRIDCELL_elementRightGridLine = "PART_RightGridLine";
@ -216,6 +216,8 @@ namespace Avalonia.Controls
PseudoClasses.Set(":edited", IsEdited); PseudoClasses.Set(":edited", IsEdited);
PseudoClasses.Set(":invalid", !IsValid); PseudoClasses.Set(":invalid", !IsValid);
PseudoClasses.Set(":focus", OwningGrid.IsFocused && IsCurrent);
} }
// Makes sure the right gridline has the proper stroke and visibility. If lastVisibleColumn is specified, the // Makes sure the right gridline has the proper stroke and visibility. If lastVisibleColumn is specified, the
@ -245,9 +247,15 @@ namespace Avalonia.Controls
if (column == null) if (column == null)
{ {
Classes.Clear(); Classes.Clear();
ClearValue(ThemeProperty);
} }
else else
{ {
if (Theme != column.CellTheme)
{
Theme = column.CellTheme;
}
Classes.Replace(column.CellStyleClasses); Classes.Replace(column.CellStyleClasses);
} }
} }

20
src/Avalonia.Controls.DataGrid/DataGridColumn.cs

@ -16,6 +16,7 @@ using Avalonia.Controls.Templates;
using Avalonia.Controls.Utils; using Avalonia.Controls.Utils;
using Avalonia.Layout; using Avalonia.Layout;
using Avalonia.Markup.Xaml.MarkupExtensions; using Avalonia.Markup.Xaml.MarkupExtensions;
using Avalonia.Styling;
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
@ -36,6 +37,7 @@ namespace Avalonia.Controls
private IControl _editingElement; private IControl _editingElement;
private ICellEditBinding _editBinding; private ICellEditBinding _editBinding;
private IBinding _clipboardContentBinding; private IBinding _clipboardContentBinding;
private ControlTheme _cellTheme;
private readonly Classes _cellStyleClasses = new Classes(); private readonly Classes _cellStyleClasses = new Classes();
/// <summary> /// <summary>
@ -399,6 +401,24 @@ namespace Avalonia.Controls
} }
} }
} }
/// <summary>
/// Backing field for CellTheme property.
/// </summary>
public static readonly DirectProperty<DataGridColumn, ControlTheme> CellThemeProperty =
AvaloniaProperty.RegisterDirect<DataGridColumn, ControlTheme>(
nameof(CellTheme),
o => o.CellTheme,
(o, v) => o.CellTheme = v);
/// <summary>
/// Gets or sets the <see cref="DataGridColumnHeader"/> cell theme.
/// </summary>
public ControlTheme CellTheme
{
get { return _cellTheme; }
set { SetAndRaise(CellThemeProperty, ref _cellTheme, value); }
}
/// <summary> /// <summary>
/// Backing field for Header property /// Backing field for Header property

1
src/Avalonia.Controls.DataGrid/DataGridRowGroupHeader.cs

@ -113,7 +113,6 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
public DataGridRowGroupHeader() public DataGridRowGroupHeader()
{ {
//DefaultStyleKey = typeof(DataGridRowGroupHeader);
AddHandler(InputElement.PointerPressedEvent, (s, e) => DataGridRowGroupHeader_PointerPressed(e), handledEventsToo: true); AddHandler(InputElement.PointerPressedEvent, (s, e) => DataGridRowGroupHeader_PointerPressed(e), handledEventsToo: true);
} }

28
src/Avalonia.Controls.DataGrid/DataGridTextColumn.cs

@ -11,6 +11,7 @@ using System.ComponentModel;
using Avalonia.Layout; using Avalonia.Layout;
using Avalonia.Markup.Xaml.MarkupExtensions; using Avalonia.Markup.Xaml.MarkupExtensions;
using Avalonia.Controls.Documents; using Avalonia.Controls.Documents;
using Avalonia.Styling;
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
@ -19,12 +20,20 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
public class DataGridTextColumn : DataGridBoundColumn public class DataGridTextColumn : DataGridBoundColumn
{ {
private readonly Lazy<ControlTheme> _cellTextBoxTheme;
private readonly Lazy<ControlTheme> _cellTextBlockTheme;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridTextColumn" /> class. /// Initializes a new instance of the <see cref="T:Avalonia.Controls.DataGridTextColumn" /> class.
/// </summary> /// </summary>
public DataGridTextColumn() public DataGridTextColumn()
{ {
BindingTarget = TextBox.TextProperty; BindingTarget = TextBox.TextProperty;
_cellTextBoxTheme = new Lazy<ControlTheme>(() =>
OwningGrid.TryFindResource("DataGridCellTextBoxTheme", out var theme) ? (ControlTheme)theme : null);
_cellTextBlockTheme = new Lazy<ControlTheme>(() =>
OwningGrid.TryFindResource("DataGridCellTextBlockTheme", out var theme) ? (ControlTheme)theme : null);
} }
/// <summary> /// <summary>
@ -156,16 +165,19 @@ namespace Avalonia.Controls
protected override IControl GenerateEditingElementDirect(DataGridCell cell, object dataItem) protected override IControl GenerateEditingElementDirect(DataGridCell cell, object dataItem)
{ {
var textBox = new TextBox var textBox = new TextBox
{ {
VerticalAlignment = VerticalAlignment.Stretch, Name = "CellTextBox"
Background = new SolidColorBrush(Colors.Transparent)
}; };
if (_cellTextBoxTheme.Value is { } theme)
{
textBox.Theme = theme;
}
SyncProperties(textBox); SyncProperties(textBox);
return textBox; return textBox;
} }
/// <summary> /// <summary>
/// Gets a read-only <see cref="T:Avalonia.Controls.TextBlock" /> element that is bound to the column's <see cref="P:Avalonia.Controls.DataGridBoundColumn.Binding" /> property value. /// Gets a read-only <see cref="T:Avalonia.Controls.TextBlock" /> element that is bound to the column's <see cref="P:Avalonia.Controls.DataGridBoundColumn.Binding" /> property value.
/// </summary> /// </summary>
@ -174,10 +186,14 @@ namespace Avalonia.Controls
/// <returns>A new, read-only <see cref="T:Avalonia.Controls.TextBlock" /> element that is bound to the column's <see cref="P:Avalonia.Controls.DataGridBoundColumn.Binding" /> property value.</returns> /// <returns>A new, read-only <see cref="T:Avalonia.Controls.TextBlock" /> element that is bound to the column's <see cref="P:Avalonia.Controls.DataGridBoundColumn.Binding" /> property value.</returns>
protected override IControl GenerateElement(DataGridCell cell, object dataItem) protected override IControl GenerateElement(DataGridCell cell, object dataItem)
{ {
TextBlock textBlockElement = new TextBlock var textBlockElement = new TextBlock
{ {
Name = "CellTextBlock" Name = "CellTextBlock"
}; };
if (_cellTextBlockTheme.Value is { } theme)
{
textBlockElement.Theme = theme;
}
SyncProperties(textBlockElement); SyncProperties(textBlockElement);
@ -227,7 +243,7 @@ namespace Avalonia.Controls
{ {
if (element == null) if (element == null)
{ {
throw new ArgumentNullException("element"); throw new ArgumentNullException(nameof(element));
} }
if (element is AvaloniaObject content) if (element is AvaloniaObject content)

1078
src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml

File diff suppressed because it is too large
Loading…
Cancel
Save