我还是Databinding的新手,现在已经阅读并研究了这个问题几个小时了,我希望有人至少能指出我正确的方向。
我所拥有的是一个填充了以下对象的DataTable:
public class SimpleObject
{
public string DisplayValue { get; set; }
public bool Match { get; set; }
public string BackGroundColor
{
get { if (Match) return "Green"; else return "White"; }
set { //do nothing }
}
}
我已经为数据表的列设置了我的标题:
DataTable MyDataTable = new DataTable()
List headers = new List<string>() {"Header1", "Header2", "Header3", "Header4"}
foreach (string key in headers)
{
MyDataTable.Columns.Add(new DataColumn(key, typeof(SimpleObject)));
}
通过添加类似于:
的行来填充我的DataTable行SimpleObject[] rowList = new SimpleObject[4]
DataRow dataRow = MyDataTable.NewRow();
for(int i = 0; i < 4; i++)
{
//Not really how I determine values, but this will do for a basic example
rowList[i].DisplayValue = i.ToString();
rowList[i].Match = i % 2 == 0;
}
dataRow.ItemArray = rowList;
MyDataTable.Rows.Add(dataRow);
SimpleDataGrid.DataContext = MyDataTable;
现在,我想要做的事情是将MyDataTable绑定到DataGrid,以便:
如果有人能就此如何向我提出建议,我将非常感激!到目前为止,我已经尝试过类似的事情:
<DataGrid Name="SimpleDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Header1" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Header1.DisplayValue}"
BackGround="{Binding Path=Header1.BackGroundColor}"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
但没有运气。我甚至无法将DisplayValue绑定到Text(即使没有尝试绑定BackGroundColor)。任何帮助或方向将不胜感激!
答案 0 :(得分:1)
我试过复制你的问题,但似乎对我来说很好。
我已向DataGrid
添加了2列,DataTable
添加了1个项目,因此将创建1行。
TextBlocks
正确绑定了Text
和Background
属性。
这是我使用过的源代码。看看它是否适合你。
XAML:
<DataGrid Name="SimpleDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Header1" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Header1.DisplayValue}"
Background="{Binding Path=Header1.BackGroundColor}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Header2" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Header2.DisplayValue}"
Background="{Binding Path=Header2.BackGroundColor}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
代码隐藏:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataTable MyDataTable = new DataTable();
List<string> headers = new List<string>() { "Header1", "Header2", "Header3", "Header4" };
foreach (string key in headers)
{
MyDataTable.Columns.Add(new DataColumn(key, typeof(SimpleObject)));
}
SimpleObject[] rowList = new SimpleObject[4];
DataRow dataRow = MyDataTable.NewRow();
for (int i = 0; i < 4; i++)
{
//Not really how I determine values, but this will do for a basic example
rowList[i] = new SimpleObject();
rowList[i].DisplayValue = i.ToString();
rowList[i].Match = i % 2 == 0;
}
dataRow.ItemArray = rowList;
MyDataTable.Rows.Add(dataRow);
SimpleDataGrid.DataContext = MyDataTable;
}
}
public class SimpleObject
{
public string DisplayValue { get; set; }
public bool Match { get; set; }
public string BackGroundColor
{
get
{
if (Match)
return "Green";
else return "Blue";
}
}
}