Browse Source

DataGridTextColumn: Make FontFamily, FontSize, FontStyle, FontWeight and Foreground properties bindable

pull/4651/head
Maksym Katsydan 6 years ago
parent
commit
f33e12c229
  1. 5
      src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt
  2. 231
      src/Avalonia.Controls.DataGrid/DataGridTextColumn.cs
  3. 22
      src/Avalonia.Controls.DataGrid/Utils/DataGridHelper.cs

5
src/Avalonia.Controls.DataGrid/ApiCompatBaseline.txt

@ -0,0 +1,5 @@
Compat issues with assembly Avalonia.Controls.DataGrid:
MembersMustExist : Member 'public Avalonia.StyledProperty<System.String> Avalonia.StyledProperty<System.String> Avalonia.Controls.DataGridTextColumn.FontFamilyProperty' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.String Avalonia.Controls.DataGridTextColumn.FontFamily.get()' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public void Avalonia.Controls.DataGridTextColumn.FontFamily.set(System.String)' does not exist in the implementation but it does exist in the contract.
Total Issues: 3

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

@ -20,11 +20,6 @@ namespace Avalonia.Controls
{ {
private const string DATAGRID_TextColumnCellTextBlockMarginKey = "DataGridTextColumnCellTextBlockMargin"; private const string DATAGRID_TextColumnCellTextBlockMarginKey = "DataGridTextColumnCellTextBlockMargin";
private double? _fontSize;
private FontStyle? _fontStyle;
private FontWeight? _fontWeight;
private IBrush _foreground;
/// <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>
@ -36,18 +31,24 @@ namespace Avalonia.Controls
/// <summary> /// <summary>
/// Identifies the FontFamily dependency property. /// Identifies the FontFamily dependency property.
/// </summary> /// </summary>
public static readonly StyledProperty<string> FontFamilyProperty = public static readonly AttachedProperty<FontFamily> FontFamilyProperty =
AvaloniaProperty.Register<DataGridTextColumn, string>(nameof(FontFamily)); TextBlock.FontFamilyProperty.AddOwner<DataGridTextColumn>();
/// <summary> /// <summary>
/// Gets or sets the font name. /// Gets or sets the font name.
/// </summary> /// </summary>
public string FontFamily public FontFamily FontFamily
{ {
get { return GetValue(FontFamilyProperty); } get => GetValue(FontFamilyProperty);
set { SetValue(FontFamilyProperty, value); } set => SetValue(FontFamilyProperty, value);
} }
/// <summary>
/// Identifies the FontSize dependency property.
/// </summary>
public static readonly AttachedProperty<double> FontSizeProperty =
TextBlock.FontSizeProperty.AddOwner<DataGridTextColumn>();
/// <summary> /// <summary>
/// Gets or sets the font size. /// Gets or sets the font size.
/// </summary> /// </summary>
@ -55,74 +56,66 @@ namespace Avalonia.Controls
[DefaultValue(double.NaN)] [DefaultValue(double.NaN)]
public double FontSize public double FontSize
{ {
get get => GetValue(FontSizeProperty);
{ set => SetValue(FontSizeProperty, value);
return _fontSize ?? Double.NaN;
}
set
{
if (_fontSize != value)
{
_fontSize = value;
NotifyPropertyChanged(nameof(FontSize));
}
}
} }
/// <summary>
/// Identifies the FontStyle dependency property.
/// </summary>
public static readonly AttachedProperty<FontStyle> FontStyleProperty =
TextBlock.FontStyleProperty.AddOwner<DataGridTextColumn>();
/// <summary> /// <summary>
/// Gets or sets the font style. /// Gets or sets the font style.
/// </summary> /// </summary>
public FontStyle FontStyle public FontStyle FontStyle
{ {
get get => GetValue(FontStyleProperty);
{ set => SetValue(FontStyleProperty, value);
return _fontStyle ?? FontStyle.Normal;
}
set
{
if (_fontStyle != value)
{
_fontStyle = value;
NotifyPropertyChanged(nameof(FontStyle));
}
}
} }
/// <summary>
/// Identifies the FontWeight dependency property.
/// </summary>
public static readonly AttachedProperty<FontWeight> FontWeightProperty =
TextBlock.FontWeightProperty.AddOwner<DataGridTextColumn>();
/// <summary> /// <summary>
/// Gets or sets the font weight or thickness. /// Gets or sets the font weight or thickness.
/// </summary> /// </summary>
public FontWeight FontWeight public FontWeight FontWeight
{ {
get get => GetValue(FontWeightProperty);
{ set => SetValue(FontWeightProperty, value);
return _fontWeight ?? FontWeight.Normal;
}
set
{
if (_fontWeight != value)
{
_fontWeight = value;
NotifyPropertyChanged(nameof(FontWeight));
}
}
} }
/// <summary>
/// Identifies the Foreground dependency property.
/// </summary>
public static readonly AttachedProperty<IBrush> ForegroundProperty =
TextBlock.ForegroundProperty.AddOwner<DataGridTextColumn>();
/// <summary> /// <summary>
/// Gets or sets a brush that describes the foreground of the column cells. /// Gets or sets a brush that describes the foreground of the column cells.
/// </summary> /// </summary>
public IBrush Foreground public IBrush Foreground
{ {
get get => GetValue(ForegroundProperty);
{ set => SetValue(ForegroundProperty, value);
return _foreground; }
}
set protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);
if (change.Property == FontFamilyProperty
|| change.Property == FontSizeProperty
|| change.Property == FontStyleProperty
|| change.Property == FontWeightProperty
|| change.Property == ForegroundProperty)
{ {
if (_foreground != value) NotifyPropertyChanged(change.Property.Name);
{
_foreground = value;
NotifyPropertyChanged(nameof(Foreground));
}
} }
} }
@ -154,26 +147,7 @@ namespace Avalonia.Controls
Background = new SolidColorBrush(Colors.Transparent) Background = new SolidColorBrush(Colors.Transparent)
}; };
if (IsSet(FontFamilyProperty)) SyncProperties(textBox);
{
textBox.FontFamily = FontFamily;
}
if (_fontSize.HasValue)
{
textBox.FontSize = _fontSize.Value;
}
if (_fontStyle.HasValue)
{
textBox.FontStyle = _fontStyle.Value;
}
if (_fontWeight.HasValue)
{
textBox.FontWeight = _fontWeight.Value;
}
if (_foreground != null)
{
textBox.Foreground = _foreground;
}
return textBox; return textBox;
} }
@ -192,26 +166,8 @@ namespace Avalonia.Controls
VerticalAlignment = VerticalAlignment.Center VerticalAlignment = VerticalAlignment.Center
}; };
if (IsSet(FontFamilyProperty)) SyncProperties(textBlockElement);
{
textBlockElement.FontFamily = FontFamily;
}
if (_fontSize.HasValue)
{
textBlockElement.FontSize = _fontSize.Value;
}
if (_fontStyle.HasValue)
{
textBlockElement.FontStyle = _fontStyle.Value;
}
if (_fontWeight.HasValue)
{
textBlockElement.FontWeight = _fontWeight.Value;
}
if (_foreground != null)
{
textBlockElement.Foreground = _foreground;
}
if (Binding != null) if (Binding != null)
{ {
textBlockElement.Bind(TextBlock.TextProperty, Binding); textBlockElement.Bind(TextBlock.TextProperty, Binding);
@ -261,99 +217,42 @@ namespace Avalonia.Controls
throw new ArgumentNullException("element"); throw new ArgumentNullException("element");
} }
if(element is TextBox textBox) if (element is AvaloniaObject content)
{ {
if (propertyName == nameof(FontFamily)) if (propertyName == nameof(FontFamily))
{ {
textBox.FontFamily = FontFamily; DataGridHelper.SyncColumnProperty(this, content, FontFamilyProperty);
} }
else if (propertyName == nameof(FontSize)) else if (propertyName == nameof(FontSize))
{ {
SetTextFontSize(textBox, TextBox.FontSizeProperty); DataGridHelper.SyncColumnProperty(this, content, FontSizeProperty);
} }
else if (propertyName == nameof(FontStyle)) else if (propertyName == nameof(FontStyle))
{ {
textBox.FontStyle = FontStyle; DataGridHelper.SyncColumnProperty(this, content, FontStyleProperty);
} }
else if (propertyName == nameof(FontWeight)) else if (propertyName == nameof(FontWeight))
{ {
textBox.FontWeight = FontWeight; DataGridHelper.SyncColumnProperty(this, content, FontWeightProperty);
} }
else if (propertyName == nameof(Foreground)) else if (propertyName == nameof(Foreground))
{ {
textBox.Foreground = Foreground; DataGridHelper.SyncColumnProperty(this, content, ForegroundProperty);
}
else
{
if (FontFamily != null)
{
textBox.FontFamily = FontFamily;
}
SetTextFontSize(textBox, TextBox.FontSizeProperty);
textBox.FontStyle = FontStyle;
textBox.FontWeight = FontWeight;
if (Foreground != null)
{
textBox.Foreground = Foreground;
}
}
}
else if (element is TextBlock textBlock)
{
if (propertyName == nameof(FontFamily))
{
textBlock.FontFamily = FontFamily;
}
else if (propertyName == nameof(FontSize))
{
SetTextFontSize(textBlock, TextBlock.FontSizeProperty);
}
else if (propertyName == nameof(FontStyle))
{
textBlock.FontStyle = FontStyle;
}
else if (propertyName == nameof(FontWeight))
{
textBlock.FontWeight = FontWeight;
}
else if (propertyName == nameof(Foreground))
{
textBlock.Foreground = Foreground;
}
else
{
if (FontFamily != null)
{
textBlock.FontFamily = FontFamily;
}
SetTextFontSize(textBlock, TextBlock.FontSizeProperty);
textBlock.FontStyle = FontStyle;
textBlock.FontWeight = FontWeight;
if (Foreground != null)
{
textBlock.Foreground = Foreground;
}
} }
} }
else else
{ {
throw DataGridError.DataGrid.ValueIsNotAnInstanceOfEitherOr("element", typeof(TextBox), typeof(TextBlock)); throw DataGridError.DataGrid.ValueIsNotAnInstanceOf("element", typeof(AvaloniaObject));
} }
} }
private void SetTextFontSize(AvaloniaObject textElement, AvaloniaProperty fontSizeProperty) private void SyncProperties(AvaloniaObject content)
{ {
double newFontSize = FontSize; DataGridHelper.SyncColumnProperty(this, content, FontFamilyProperty);
if (double.IsNaN(newFontSize)) DataGridHelper.SyncColumnProperty(this, content, FontSizeProperty);
{ DataGridHelper.SyncColumnProperty(this, content, FontStyleProperty);
textElement.ClearValue(fontSizeProperty); DataGridHelper.SyncColumnProperty(this, content, FontWeightProperty);
} DataGridHelper.SyncColumnProperty(this, content, ForegroundProperty);
else
{
textElement.SetValue(fontSizeProperty, newFontSize);
}
} }
} }
} }

22
src/Avalonia.Controls.DataGrid/Utils/DataGridHelper.cs

@ -0,0 +1,22 @@
namespace Avalonia.Controls
{
public static class DataGridHelper
{
internal static void SyncColumnProperty<T>(AvaloniaObject column, AvaloniaObject content, AvaloniaProperty<T> property)
{
SyncColumnProperty(column, content, property, property);
}
internal static void SyncColumnProperty<T>(AvaloniaObject column, AvaloniaObject content, AvaloniaProperty<T> contentProperty, AvaloniaProperty<T> columnProperty)
{
if (!column.IsSet(columnProperty))
{
content.ClearValue(contentProperty);
}
else
{
content.SetValue(contentProperty, column.GetValue(columnProperty));
}
}
}
}
Loading…
Cancel
Save