使用Synchfusion的SfDataGrid for UWP将文本居中,并带有动态列

时间:2019-03-13 07:13:23

标签: uwp datagrid syncfusion

我试图在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}" -->

但这无济于事,因为它使整个单元格居中,并且网格的内部边界被破坏。 (如下图所示)

enter image description here

我只希望单元格内的文本对齐。 (也尝试过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正常工作,就可以实现我的目标。

1 个答案:

答案 0 :(得分:1)

“ 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"));
    }
}