我有一个DataTable
,它在单元格级别填充了以下类型:
public class GridCell
{
public string Value { get; set}
public Guid Id { get; set; }
}
我已经将DataTable绑定到WPF Toolkit DataGrid,但是单元格是空的。如何告诉DataGrid
查看我的Value
类型的GridCell
属性中的单元格内容?
绑定摘录:
<DataGrid ItemsSource="{Binding SelectedItem.Table}" />
答案 0 :(得分:1)
根据你问题中的有限信息,我猜你的数据表包含我不推荐的自定义类型的列,因为它会使你的代码变得更加复杂。但是如果你决定采用这种方式,你可以实现自定义列类型来处理它。我没有尝试过,但这个链接看起来很有希望:Bind DataTable to WPF DataGrid using DataGridTemplateColumn Programatically
您还可以使用值转换器来实现相同的功能。下面的示例将列索引作为参数传递,但您也可以使用某种格式传递您感兴趣的属性(例如0-Value,意思是第0列,属性值)。
XAML:
<Window x:Class="GridCellDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
xmlns:GridCellDemo="clr-namespace:GridCellDemo"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<GridCellDemo:CellConverter x:Key="CellConverter" />
</Window.Resources>
<Grid>
<Controls:DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="False">
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn Header="Col 0" Binding="{Binding ., Converter={StaticResource CellConverter}, ConverterParameter=0}" />
<Controls:DataGridTextColumn Header="Col 1" Binding="{Binding ., Converter={StaticResource CellConverter}, ConverterParameter=1}" />
</Controls:DataGrid.Columns>
</Controls:DataGrid>
</Grid>
</Window>
代码背后:
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
namespace GridCellDemo
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = this;
}
public DataTable Data
{
get
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Col0", typeof(GridCell)));
dt.Columns.Add(new DataColumn("Col1", typeof(GridCell)));
DataRow row0 = dt.NewRow();
dt.Rows.Add(row0);
row0["Col0"] = new GridCell() { Value = "R0C0" };
row0["Col1"] = new GridCell() { Value = "R0C1" };
DataRow row1 = dt.NewRow();
dt.Rows.Add(row1);
row1["Col0"] = new GridCell() { Value = "R1C0" };
row1["Col1"] = new GridCell() { Value = "R1C1" };
return dt;
}
}
}
public class GridCell
{
public string Value { get; set; }
public Guid Id { get; set; }
}
public class CellConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DataRowView drv = value as DataRowView;
if (drv == null)
{
return string.Empty;
}
int columnIndex = int.Parse(parameter.ToString());
GridCell gridCell = drv[columnIndex] as GridCell;
return gridCell.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
答案 1 :(得分:0)
尝试ItemsSource="{Binding Path=GridCellList}"
。
<DataGrid ItemsSource="{Binding GridCellList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Value" Binding="{Binding Path=Value}" />
<DataGridTextColumn Header="Guid" Binding="{Binding Path=Guid}" />
</DataGrid.Columns>
</DataGrid>