[英]Programmatically bind DataGridTemplateColumn
Here is my code: 这是我的代码:
foreach (var columnData in lookup.DataProvider.Metadata)
{
DataGridColumn column = new DataGridTextColumn { Binding = new Binding(columnData.FieldName) };
if (columnData.DataType == typeof(bool))
{
column = new DataGridCheckBoxColumn { Binding = new Binding(columnData.FieldName) };
}
if (columnData.DataType == typeof(DateTime))
{
column = new DataGridTemplateColumn();
//... ????
}
column.Header = columnData.Caption;
DataDataGrid.Columns.Add(column);
}
Basically, I'm creating columns and bindings in code because columns not known at design-time. 基本上,我在代码中创建列和绑定,因为列在设计时是未知的。
Now I need to add templated column and not sure how to write it in C#. 现在我需要添加模板化列,而不确定如何在C#中编写它。 Here is example of XAML of column I need to add:
以下是我需要添加的列的XAML示例:
<sdk:DataGridTemplateColumn Header="Received" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" SortMemberPath="SomeTime">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<MyControls:MyDateTimeLabel DisplayUtcDate="{Binding SomeTime}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
EDIT 编辑
In case someone interested. 万一有人感兴趣。 I used solution from here: http://www.pettijohn.com/2011/01/silverlight-datagrid-with-dynamic.html
我在这里使用了解决方案: http : //www.pettijohn.com/2011/01/silverlight-datagrid-with-dynamic.html
I took version with XAML loader. 我用XAML加载器购买了版本。 It definitely smells since I got my namespaces etc hardcoded into strings.
它确实闻起来,因为我将我的命名空间等硬编码到字符串中。
So, I started to explore second choice. 所以,我开始探索第二选择。 Here is how my dynamic column looks now:
以下是我的动态列现在的样子:
column = new DataGridTemplateColumn
{
CanUserSort = true,
SortMemberPath = columnData.FieldName,
CellTemplate = (DataTemplate)this.Resources["DateTimeColumnDataTemplate"]
};
I'm loading DateTemplate
from resources. 我正在从资源加载
DateTemplate
。 This was cool, but how do I do binding? 这很酷,但我该怎么做绑定? Suggestion here was to get to my DateTimeLabel and set binding.
这里的建议是到达我的DateTimeLabel并设置绑定。 But that didn't work(see article on why).
但那不起作用(见有关原因的文章)。 So, I wrote this code and all is well:
所以,我写了这段代码,一切都很好:
private void OnLoadingRow(object sender, DataGridRowEventArgs e)
{
foreach (DataGridColumn t in this.DataDataGrid.Columns)
{
if (t is DataGridTemplateColumn)
{
var label = t.GetCellContent(e.Row) as DitatDateTimeLabel;
label.SetBinding(DitatDateTimeLabel.DisplayUtcDateProperty, new Binding(t.SortMemberPath));
}
}
}
You could put your DataTemplate
inside Page
/ UserControl
resources, retrieve it in code and apply to your column's CellTemplate
. 您可以将
DataTemplate
放在Page
/ UserControl
资源中,在代码中检索它并应用于列的CellTemplate
。 It would look sth like this: 看起来像这样:
column.CellTemplate = (DataTemplate)this.Resources["DateTimeFieldTemplate"];
The binding should work as it is in your DataTemplate
XAML right now because on the DataGrid
row level your DataContext
will be set to the item itself. 绑定应该像在
DataTemplate
XAML中一样工作,因为在DataGrid
行级别, DataContext
将被设置为项目本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.