使用 DataGrid 列转换器
我使用 MVVM 模式创建 Windows 数据库基础设施。我有为我的 DataGrid 返回数据的方法。 问题是,我希望 DataGrid 中的某些列使用转换器 (IValueConverter),但我将 DataGrid 直接绑定到数据。我无法在 xaml 中强类型列规范,因为列数可能不同。另外,我无法在 ViewModel 中获取 UI 元素(DataGrid)进行更改。
I create my windows databainding infrastructure with MVVM pattern. I have method which return data for my DataGrid. Problem that I want some columns in DataGrid use Converter (IValueConverter) but I bind my DataGrid to the data directly. I can't strongly type columns specification in xaml because number of columns can be different. Also I can't get UI element (DataGrid) in ViewModel for change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 ViewModel 可以公开列集合(可以指定列标题、数据类型、使用转换器的必要信息的结构)将其绑定到视图的 DP,并且您的视图需要在解析该集合并准备数据模板后将列添加到数据网格对于数据网格列的单元格模板和单元格编辑模板,指定绑定源、转换器。
Your ViewModel can expose the column collection (structure which can specify column header, type of data, necessary information to use converter) bind that to a DP of your view and your view needs to add the columns to the datagrid after parsing that and preparing datatemplate for datagrid column's cell template and cell edittemplate which specifies the binding source, converter.