使用Synchfusion'将文本居中;具有动态列的UWP的SfDataGrid
我正在尝试将UWP中SfDataGrid的文本置于单元格的中心。这些列是在运行时绑定的,因此我无法在列元素上设置单元格样式 网格的xaml如下所示:使用Synchfusion'将文本居中;具有动态列的UWP的SfDataGrid,uwp,datagrid,syncfusion,Uwp,Datagrid,Syncfusion,我正在尝试将UWP中SfDataGrid的文本置于单元格的中心。这些列是在运行时绑定的,因此我无法在列元素上设置单元格样式 网格的xaml如下所示: <grid:SfDataGrid Name="GridData" AlternatingRowStyle="{StaticResource mainTableRowStyle}"
<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>
<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;
}
}
我已尝试向单元格添加样式,以便对齐文本:
<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}" -->
但这没有帮助,因为它使整个细胞居中,网格的内部边界被破坏。(看起来有点像下图)
我只希望单元格内的文本对齐。(还尝试了HorizontalContentAlignment center,但什么也没做)
最后,我尝试重写单元格的模板。SfDataGrid没有CellTemplate属性,但有GridCellTemplateSelector属性。因此,我创建了如下模板:
<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>
<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:DataTemplateSelector
{
受保护的覆盖数据模板SelectTemplateCore(对象项,DependencyObject容器)
{
返回Application.Current.Resources[“CellTemplate1”]作为DataTemplate;
}
}
这个方法也不起作用,因为它似乎没有命中GridCellTemplateSelector中的方法。我在想,如果我能让CellTemplateSelector发挥作用,我就能实现我的目标。GridCellTemplateSelector不适合您的场景。sfDataGrid具有“CellTemplate”,可用于列 我制作了一个代码示例供您参考:
公共类OrderInfo
{
int-orderID;
字符串customerId;
字符串客户名称;
公共整型医嘱ID
{
获取{return orderID;}
设置{orderID=value;}
}
公共字符串自定义ID
{
获取{return customerId;}
设置{customerId=value;}
}
公共字符串客户名称
{
获取{return customerName;}
设置{customerName=value;}
}
public OrderInfo(int-orderId、字符串customerName、字符串customerId)
{
this.OrderID=OrderID;
this.CustomerName=客户名称;
this.CustomerID=CustomerID;
}
}
公共密封部分类主页面:第页
{
私人可观测收款单;
公开征收令
{
获取{返回_订单;}
设置{u orders=value;}
}
公共主页()
{
this.InitializeComponent();
_订单=新的ObservableCollection();
这个.GenerateOrders();
this.DataContext=this;
类型mytype=Orders.FirstOrDefault().GetType();
foreach(mytype.GetProperties()中的PropertyInfo pi)
{
dataGrid.Columns.Add(new GridTextColumn(){MappingName=pi.Name,TextAlignment=TextAlignment.Center,HeaderText=pi.Name});
}
}
私有void GenerateOrders()
{
_添加(新订单信息(1001,“Maria Anders”,“ALFKI”);
_添加(新订单信息(1002,“Ana Trujillo”,“Anar”);
}
}
谢谢您的示例。但是,正如我在问题中所描述的,这些列在运行时是动态的和绑定的,这就是为什么我不能在这个示例中使用类似的东西。这是一个通用网格,根据数据可以有一列、两列或更多列。我想要一种将所有列中的所有内容居中对齐的方法。受您的回答启发,我在创建列时通过设置代码中的TextAlignment属性来解决这个问题。你能用{for(var i=0;i