如何将 DataTemplateSelector 与 DataGridBoundColumn 一起使用?
我遵循此处描述的简单方法,并拥有一个带有动态生成列的 DataGrid,允许使用 DataTemplates并动态绑定。
for (int i = 0; i < testDataGridSourceList.DataList[0].Count; i++)
{
var binding = new Binding(string.Format("[{0}]", i));
CustomBoundColumn customBoundColumn = new CustomBoundColumn();
customBoundColumn.Header = "Col" + i;
customBoundColumn.Binding = binding;
customBoundColumn.TemplateName = "CustomTemplate";
TestControlDataGrid.TestDataGrid.Columns.Add(customBoundColumn);
}
每列都是 CustomBoundColumn 类型,它派生自 DataGridBoundColumn
public class CustomBoundColumn : DataGridBoundColumn
{
public string TemplateName { get; set; }
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
var binding = new Binding(((Binding)Binding).Path.Path);
binding.Source = dataItem;
var content = new ContentControl();
content.ContentTemplate = (DataTemplate)cell.FindResource(TemplateName);
content.SetBinding(ContentControl.ContentProperty, binding);
return content;
}
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
{
return GenerateElement(cell, dataItem);
}
}
我现在想使用 DataTemplateSelector 来允许每行使用不同的 DataTemplate,而不是仅使用第一个代码段中显示的“CustomTemplate”。我该怎么做?
I followed the simple method described here and have a DataGrid with dynamically generated columns which allows DataTemplates to be used and bound dynamically.
for (int i = 0; i < testDataGridSourceList.DataList[0].Count; i++)
{
var binding = new Binding(string.Format("[{0}]", i));
CustomBoundColumn customBoundColumn = new CustomBoundColumn();
customBoundColumn.Header = "Col" + i;
customBoundColumn.Binding = binding;
customBoundColumn.TemplateName = "CustomTemplate";
TestControlDataGrid.TestDataGrid.Columns.Add(customBoundColumn);
}
Each column is of type CustomBoundColumn which derives from DataGridBoundColumn
public class CustomBoundColumn : DataGridBoundColumn
{
public string TemplateName { get; set; }
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
var binding = new Binding(((Binding)Binding).Path.Path);
binding.Source = dataItem;
var content = new ContentControl();
content.ContentTemplate = (DataTemplate)cell.FindResource(TemplateName);
content.SetBinding(ContentControl.ContentProperty, binding);
return content;
}
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
{
return GenerateElement(cell, dataItem);
}
}
I would now like to use a DataTemplateSelector to allow each row to use a different DataTemplate instead of just using the "CustomTemplate" shown in the first snippet. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉回复晚了。我相信解决方案非常简单,只需在“CustomTemplate”中放置一个
ContentPresenter
即可:就这样!您现在可以使用
DataTemplateSelector
。 此处就是一个很好的示例。Sorry for the late answer. I believe the solution is quite simple, just place a
ContentPresenter
in your "CustomTemplate" :And there you go! You can now use a
DataTemplateSelector
. A good example here.最后,我替换
为
其中“templateSelector”是在 XAML 代码中声明为静态资源的 DataTemplateSelector 的键。这很好用。
In the end I replaced
with
where 'templateSelector' is the key of a DataTemplateSelector declared as a Static Resource in the XAML code. This works fine.
我创建了一个自定义列类,将 DataGridBoundColumn 与 DataGridTemplateColumn 组合在一起。
您可以在该列上设置绑定和模板。
这是来源:
要点
I made a custom column class that combines the DataGridBoundColumn with the DataGridTemplateColumn.
You can set Binding and Template on that column.
Here's the source:
gist