Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property TextAlignment in DataGridTextColumn to support align the text in rows to right #4281

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
public class DataGridTextColumn : DataGridBoundColumn
{
private const string DATAGRIDTEXTCOLUMN_fontFamilyName = "FontFamily";
private const string DATAGRIDTEXTCOLUMN_textAlignmentName = "TextAlignment";
private const string DATAGRIDTEXTCOLUMN_fontSizeName = "FontSize";
private const string DATAGRIDTEXTCOLUMN_fontStyleName = "FontStyle";
private const string DATAGRIDTEXTCOLUMN_fontWeightName = "FontWeight";
Expand Down Expand Up @@ -67,6 +68,31 @@ private static void OnFontFamilyPropertyChanged(DependencyObject d, DependencyPr
textColumn.NotifyPropertyChanged(DATAGRIDTEXTCOLUMN_fontFamilyName);
}

/// <summary>
/// Gets or sets the text alignment.
/// </summary>
public TextAlignment TextAlignment
{
get { return (TextAlignment)GetValue(TextAlignmentProperty); }
set { SetValue(TextAlignmentProperty, value); }
}

/// <summary>
/// Identifies the TextAlignment dependency property.
/// </summary>
public static readonly DependencyProperty TextAlignmentProperty =
DependencyProperty.Register(
DATAGRIDTEXTCOLUMN_textAlignmentName,
typeof(TextAlignment),
typeof(DataGridTextColumn),
new PropertyMetadata(TextAlignment.Left, OnTextAlignmentPropertyChanged));

private static void OnTextAlignmentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGridTextColumn textColumn = d as DataGridTextColumn;
textColumn.NotifyPropertyChanged(DATAGRIDTEXTCOLUMN_textAlignmentName);
}

/// <summary>
/// Gets or sets the font size.
/// </summary>
Expand Down Expand Up @@ -181,6 +207,11 @@ protected override FrameworkElement GenerateEditingElement(DataGridCell cell, ob
textBox.FontFamily = this.FontFamily;
}

if (DependencyProperty.UnsetValue != ReadLocalValue(DataGridTextColumn.TextAlignmentProperty))
{
textBox.TextAlignment = this.TextAlignment;
}

if (_fontSize.HasValue)
{
textBox.FontSize = _fontSize.Value;
Expand Down Expand Up @@ -222,6 +253,11 @@ protected override FrameworkElement GenerateElement(DataGridCell cell, object da
textBlockElement.FontFamily = this.FontFamily;
}

if (DependencyProperty.UnsetValue != ReadLocalValue(DataGridTextColumn.TextAlignmentProperty))
{
textBlockElement.TextAlignment = this.TextAlignment;
}

if (_fontSize.HasValue)
{
textBlockElement.FontSize = _fontSize.Value;
Expand Down Expand Up @@ -301,6 +337,10 @@ protected internal override void RefreshCellContent(FrameworkElement element, Br
{
textBlock.FontFamily = this.FontFamily;
}
else if (propertyName == DATAGRIDTEXTCOLUMN_textAlignmentName)
{
textBlock.TextAlignment = this.TextAlignment;
}
else if (propertyName == DATAGRIDTEXTCOLUMN_fontSizeName)
{
SetTextFontSize(textBlock, TextBlock.FontSizeProperty);
Expand All @@ -324,6 +364,11 @@ protected internal override void RefreshCellContent(FrameworkElement element, Br
textBlock.FontFamily = this.FontFamily;
}

if (this.FontFamily != null)
{
textBlock.TextAlignment = this.TextAlignment;
}

SetTextFontSize(textBlock, TextBlock.FontSizeProperty);
textBlock.FontStyle = this.FontStyle;
textBlock.FontWeight = this.FontWeight;
Expand All @@ -337,6 +382,10 @@ protected internal override void RefreshCellContent(FrameworkElement element, Br
{
textBox.FontFamily = this.FontFamily;
}
else if (propertyName == DATAGRIDTEXTCOLUMN_textAlignmentName)
{
textBox.TextAlignment = this.TextAlignment;
}
else if (propertyName == DATAGRIDTEXTCOLUMN_fontSizeName)
{
SetTextFontSize(textBox, TextBox.FontSizeProperty);
Expand All @@ -360,6 +409,11 @@ protected internal override void RefreshCellContent(FrameworkElement element, Br
textBox.FontFamily = this.FontFamily;
}

if (this.FontFamily != null)
{
textBox.TextAlignment = this.TextAlignment;
}

SetTextFontSize(textBox, TextBox.FontSizeProperty);
textBox.FontStyle = this.FontStyle;
textBox.FontWeight = this.FontWeight;
Expand Down