使用 Synchfusion 的 SfDataGrid 为具有动态列的 UWP 居中文本

Center text using Synchfusion's SfDataGrid for UWP with dynamic columns

我正在尝试将 UWP 中的 SfDataGrid 单元格中的文本居中。列在运行时绑定,因此我无法在列元素上设置单元格样式。

网格的 xaml 如下所示:

                  <grid:SfDataGrid Name="GridData"
                                     AlternatingRowStyle="{StaticResource mainTableRowStyle}"
                                     RowStyle="{StaticResource mainTableRowStyle}"
                                     HeaderStyle="{StaticResource headerStyle}"
                                     Foreground="WhiteSmoke"
                                     framework:FocusExtension.IsFocused="{Binding Focused}"
                                     AllowSelectionOnPointerPressed="True"
                                     Grid.Row="0"
                                     Columns="{Binding SfGridColumns, Mode=TwoWay}"
                                     AutoGenerateColumns="True"
                                     IsDynamicItemsSource="True"
                                     ItemsSource="{Binding ElementName=dataPager,Path=PagedSource}"
                                     ColumnSizer="Star"
                                     AllowSorting="False"
                                     SelectedItem="{Binding SelectedGridItem, Mode =TwoWay, UpdateSourceTrigger=PropertyChanged}">
                        <interactivity:Interaction.Behaviors>
                            <core:EventTriggerBehavior EventName="Holding">
                                <core:InvokeCommandAction Command="{Binding HoldCommand}" />
                            </core:EventTriggerBehavior>
                        </interactivity:Interaction.Behaviors>
                    </grid:SfDataGrid>

我尝试向单元格添加样式以对齐文本:

    <Style x:Key="cellStyle" TargetType="grid:GridCell">
        <Setter Property="FontSize" Value="20" />
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
   <!-- CellStyle="{StaticResource cellStyle}" -->

但这无济于事,因为它使整个单元格居中并且网格的内部边界被打乱了。 (看起来像下图)

我只想对齐单元格内的文本。 (也尝试了 Horizo​​ntalContentAlignment 中心,它没有做任何事情)

最后,我尝试重写了单元格的模板。 SfDataGrid 没有 CellTemplate 属性,但它有一个 GridCellTemplateSelector 属性。所以,我创建了一个这样的模板:

    <framework:GridCellTemplateSelector x:Key="templateSelector"/>
    <DataTemplate x:Key="CellTemplate1">
        <TextBlock Foreground="DarkBlue" Text="{Binding}" HorizontalAlignment="Center"/>
    </DataTemplate> <!-- and added CellTemplateSelector="{StaticResource templateSelector}" to the grid -->

public class GridCellTemplateSelector : DataTemplateSelector
    {
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            return Application.Current.Resources["CellTemplate1"] as DataTemplate;
        }
    }

这个也不起作用,因为它似乎没有命中 GridCellTemplateSelector 中的方法。我在想,如果我能让 CellTemplateSelector 工作,我就能实现我的 objective.

'GridCellTemplateSelector' 不适合您的情况。 sfDataGrid 有 'CellTemplate',可用于列。

我做了一个代码示例供大家参考:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <syncfusion:SfDataGrid x:Name="dataGrid"
                           ItemsSource="{Binding Orders}">
    </syncfusion:SfDataGrid>
</Grid>
public class OrderInfo
{
    int orderID;
    string customerId;
    string customerName;

    public int OrderID
    {
        get { return orderID; }
        set { orderID = value; }
    }

    public string CustomerID
    {
        get { return customerId; }
        set { customerId = value; }
    }

    public string CustomerName
    {
        get { return customerName; }
        set { customerName = value; }
    }



    public OrderInfo(int orderId, string customerName, string customerId)
    {
        this.OrderID = orderId;
        this.CustomerName = customerName;
        this.CustomerID = customerId;
    }
}
public sealed partial class MainPage : Page
{
    private ObservableCollection<OrderInfo> _orders;

    public ObservableCollection<OrderInfo> Orders
    {
        get { return _orders; }
        set { _orders = value; }
    }

    public MainPage()
    {
        this.InitializeComponent();
        _orders = new ObservableCollection<OrderInfo>();
        this.GenerateOrders();
        this.DataContext = this;
        Type mytype = Orders.FirstOrDefault().GetType();
        foreach (PropertyInfo pi in mytype.GetProperties())
        {
            dataGrid.Columns.Add(new GridTextColumn() { MappingName = pi.Name, TextAlignment = TextAlignment.Center, HeaderText = pi.Name });
        }
    }

    private void GenerateOrders()
    {
        _orders.Add(new OrderInfo(1001, "Maria Anders",  "ALFKI"));
        _orders.Add(new OrderInfo(1002, "Ana Trujillo",  "ANATR"));
    }
}