WPF 工具包 - 数据网格 - 带动态资源的组合框列绑定

发布于 2024-07-29 00:56:05 字数 392 浏览 5 评论 0原文

我正在实现 WPF DataGrid (对于 WPF 来说非常新)。 我遵循了演示如何使用静态资源绑定 ComboBoxColumn 的教程。 但是,我的数据网格中几列的数据绑定直到运行时才会知道。

因此,我无法将它们与静态资源绑定。 是否有其他方法可以对 DataGrid 中的 ComboBoxColumn 进行数据绑定? 在 ASP.NET 中,我知道我们有 rowdatabound 代码,我们可以在其中执行此操作并动态创建列的内容。 但是,在 WPF 中,看起来一切都是通过资源完成的。

如何使用 DataGrid 中的动态资源进行数据绑定?

谢谢!

I am implementing the WPF DataGrid (very new to WPF). I followed tutorials that showed how to bind the ComboBoxColumn using staticresources. However, the databinding for a few columns in my datagrid will not be known until runtime.

Because of this, I can't bind them with the staticresource. Is there any other way to databind the ComboBoxColumns in a DataGrid? In ASP.NET, I know we had the rowdatabound code where we could do this and dynamically create the contents of the columns. But, in WPF, it looks like everything is done through resources.

How can you databind using dynamic resources in the DataGrid?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

眼泪也成诗 2024-08-05 00:56:05

您可以动态设置绑定。
像这样的东西(此代码创建网格视图列并分配动态绑定)

       private void AddColumn(GridView view, Field fld)
        {
            GridViewColumn col = new GridViewColumn();
            col.Header = fld.Label;
            Binding bnd = new Binding();
            switch (fld.FieldType)
            {
                case FieldType.DateTime:
                bnd.Converter = new DateTimeToDateStringConverter();
                break;
// or some other converters
            }
            bnd.Path = new PropertyPath(string.Format("Fields[{0}]",
    _table._fields.IndexOf(fld)));  // the string matches what you would use in XAML
            col.DisplayMemberBinding = bnd;
            view.Columns.Add(col);
        }

You can set up bindings dynamically.
Something like this (this code creates grid view columns and assigns dynamic bindings)

       private void AddColumn(GridView view, Field fld)
        {
            GridViewColumn col = new GridViewColumn();
            col.Header = fld.Label;
            Binding bnd = new Binding();
            switch (fld.FieldType)
            {
                case FieldType.DateTime:
                bnd.Converter = new DateTimeToDateStringConverter();
                break;
// or some other converters
            }
            bnd.Path = new PropertyPath(string.Format("Fields[{0}]",
    _table._fields.IndexOf(fld)));  // the string matches what you would use in XAML
            col.DisplayMemberBinding = bnd;
            view.Columns.Add(col);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文