C# WPF数据绑定Datagrid根据数据类型更改水平列方向
我试图根据数据类型(例如Int32、float等)更改数据绑定数据网格的水平列对齐方式 在网上搜索了一个简单的例子之后,我了解到,通过xaml的DataTriggers应该是正确的选择。是这样吗?如果是,我将如何实现触发器 我是WPF的新手,过去一直在使用WindowsForms。根据数据类型更改列方向不会有那么困难吗?感谢您的帮助 这可能有助于-C# WPF数据绑定Datagrid根据数据类型更改水平列方向,c#,wpf,datagrid,orientation,databound,C#,Wpf,Datagrid,Orientation,Databound,我试图根据数据类型(例如Int32、float等)更改数据绑定数据网格的水平列对齐方式 在网上搜索了一个简单的例子之后,我了解到,通过xaml的DataTriggers应该是正确的选择。是这样吗?如果是,我将如何实现触发器 我是WPF的新手,过去一直在使用WindowsForms。根据数据类型更改列方向不会有那么困难吗?感谢您的帮助 这可能有助于- 另一个选项是使用DataTemplate选择器。请查看本教程:好的,我现在已经从代码中解决了这个问题。也许有人可以给我一个提示,我如何使用XAML更
另一个选项是使用DataTemplate选择器。请查看本教程:好的,我现在已经从代码中解决了这个问题。也许有人可以给我一个提示,我如何使用XAML更优雅地解决这个问题?我在网上搜索了几个小时,想找到一个对WPF新手来说不太难的例子,但是没有找到任何a能够成功实现的东西 好的,下面是代码:使用DataTable作为数据源,我添加了以下内容:
foreach (DataColumn cc in table.Columns)
{
Type type = cc.DataType;
Style alignStyle = new Style(typeof(Microsoft.Windows.Controls.DataGridCell));
alignStyle.Setters.Add(new Setter(Microsoft.Windows.Controls.DataGridCell.VerticalAlignmentProperty, VerticalAlignment.Center));
var column = new Microsoft.Windows.Controls.DataGridTextColumn
{
Header = cc.ColumnName,
Binding = new Binding(cc.ColumnName)
};
if(type.Name=="Int32"){
alignStyle.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right));
column.Foreground = Brushes.Red;
column.CellStyle = alignStyle;
}
else if (type.Name == "DateTime")
{
alignStyle.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center));
column.Foreground = Brushes.Green;
column.Binding.StringFormat = "{0:dd.MM.yyyy}";
column.CellStyle = alignStyle;
}
else if (type.Name == "String")
{
alignStyle.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Left));
column.Foreground = Brushes.Blue;
column.CellStyle = alignStyle;
}
else if (type.Name == "Double")
{
alignStyle.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right));
column.Foreground = Brushes.Brown;
column.Binding.StringFormat = "{0:F3}";
column.CellStyle = alignStyle;
}
grids.Columns.Add(column);
}
您可以处理自动生成列事件 在xaml中添加:
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="True"
AutoGeneratingColumn="DataGrid_OnAutoGeneratingColumn"></DataGrid>
}
您可以查看以下链接:
感谢您的帮助:-)我查看了这两个链接,但无法在自定义网格中实现xaml。我现在已经从代码背后解决了这个问题-请参阅下面的答案。当您看到下面的代码时,也许您对如何在xaml中执行同样的操作有另一个建议?对于自动生成列的数据网格,有一个技巧。您可以在代码隐藏中处理自动生成列事件,并修改特定列的CellTemplate。我不喜欢这种方法,因为我是MVVM的忠实粉丝。我会把答案贴在下面
private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof (Int32))
{
if (e.Column != null)
{
var dgct = new DataGridTemplateColumn();
var cName = e.Column.Header as string;
var b = new Binding(cName);
var sfactory = new FrameworkElementFactory(typeof(TextBlock));
sfactory.SetValue(TextBlock.TextProperty, b);
sfactory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
var cellTemplate = new DataTemplate();
cellTemplate.VisualTree = sfactory;
dgct.CellTemplate = cellTemplate;
dgct.Header = cName;
dgct.SortMemberPath = cName;
e.Column = dgct;
}
}
... *and so on for all your data types*