如何将 DataTemplateSelector 与 DataGridBoundColumn 一起使用?

发布于 2024-12-07 16:14:01 字数 1515 浏览 1 评论 0原文

我遵循此处描述的简单方法,并拥有一个带有动态生成列的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自我难过 2024-12-14 16:14:01

抱歉回复晚了。我相信解决方案非常简单,只需在“CustomTemplate”中放置一个 ContentPresenter 即可:

<DataTemplate x:Key="CustomTemplate">
    <ContentPresenter Content="{Binding}"
                      ContentTemplateSelector="{StaticResource myTemplateSelector}">
    </ContentPresenter>
</DataTemplate>

就这样!您现在可以使用DataTemplateSelector此处就是一个很好的示例。

Sorry for the late answer. I believe the solution is quite simple, just place a ContentPresenter in your "CustomTemplate" :

<DataTemplate x:Key="CustomTemplate">
    <ContentPresenter Content="{Binding}"
                      ContentTemplateSelector="{StaticResource myTemplateSelector}">
    </ContentPresenter>
</DataTemplate>

And there you go! You can now use a DataTemplateSelector. A good example here.

记忆で 2024-12-14 16:14:01

最后,我替换

content.ContentTemplate = (DataTemplate)cell.FindResource(TemplateName);

content.ContentTemplateSelector = (DataTemplateSelector)cell.FindResource("templateSelector");

其中“templateSelector”是在 XAML 代码中声明为静态资源的 DataTemplateSelector 的键。这很好用。

In the end I replaced

content.ContentTemplate = (DataTemplate)cell.FindResource(TemplateName);

with

content.ContentTemplateSelector = (DataTemplateSelector)cell.FindResource("templateSelector");

where 'templateSelector' is the key of a DataTemplateSelector declared as a Static Resource in the XAML code. This works fine.

数理化全能战士 2024-12-14 16:14:01

我创建了一个自定义列类,将 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文