作成日: 2021/12/09 最終更新日: 2021/12/09
文書種別
不具合
発生環境
.NET 5用のFlexGrid(C1.WPF.Grid)
状況
回避方法あり
詳細
改行を含んだテキストの中央寄せの表示が.NET Framework 4.5.2ビルドのFlexGrid(C1.WPF.FlexGrid.4.5.2)と異なります。
例えば、改行が含まれたテキストを列ヘッダに設定し、HeaderHorizontalAlignmentプロパティをCenterに指定すると、テキストの中央寄せの表示が以下のように.NET Framework 4.5.2ビルドのFlexGridと異なります。
.NET Framework 4.5.2のFlexGridの表示:
aaaaaa
bbb
.NET 5のFlexGridの表示:
aaaaaa
bbb
aaaaaa
bbb
.NET 5のFlexGridの表示:
aaaaaa
bbb
.NET 5のFlexGridの場合、テキスト全体は中央寄せになりますが、「bbb」が一行目の左側に揃えられて表示されます。
回避方法
GridColumnクラスを継承したクラスを作成し、CreateCellContentメソッドをオーバーライドする方法で回避可能です。
◎サンプルコード(C#)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_grid.Columns[0] = new GridColumnEx();
_grid.Columns[0].Header = "aaaaaa\nbbb";
_grid.Columns[0].HeaderHorizontalAlignment = HorizontalAlignment.Center;
}
}
public class GridColumnEx : GridColumn
{
protected override FrameworkElement CreateCellContent(GridCellType cellType, object cellContentType, GridRow row)
{
var element = base.CreateCellContent(cellType, cellContentType, row);
if (cellType == GridCellType.ColumnHeader)
{
(element as GridColumnHeaderCell).GetChildren().OfType().FirstOrDefault().TextAlignment = TextAlignment.Center;
}
return element;
}
}
public static class DependencyObjectEx
{
internal static IEnumerable GetChildren(this DependencyObject view, bool includeSelf = true, bool recursive = true)
{
if (view == null) yield break;
if (includeSelf) yield return view;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(view); i++)
{
var child = VisualTreeHelper.GetChild(view, i);
yield return child;
if (recursive)
{
foreach (var item in child.GetChildren(false, true))
{
yield return item;
}
}
}
}
}