WPF Datagrid 绑定自定义列标题
我试图弄清楚如何使用 MVVM 模式将 WPF DataGrid 的列标题和主要数据绑定到数据源。我正在寻找的结果如下所示:
(来源:vallelunga.com)
我已经在这里成功设置了标题的样式,但我不确定如何绑定标题中的值。具体来说,是复选框的 IsChecked 属性、组合框的选定索引和文本框的值。
我以前使用一个简单的 DataTable 来填充主网格数据,但我将需要更复杂的东西来保存网格数据和每列的值。或者也许我可以将它们完全存储为单独的实体。
那么,有人知道我如何实现这种绑定吗?一个限制是这些列必须自动生成,因为在运行时我不知道它们会是什么。该应用程序只需从 Excel 电子表格加载数据,并且可能存在任意数量的列。
谢谢, 布莱恩
I am trying to figure out how to bind a WPF DataGrid's column header and main data to a data source using an MVVM pattern. The result I'm looking for would look like this:
(source: vallelunga.com)
I've successfully styled the headers here, but I'm unsure how to bind the values in the headers. Specifically, the IsChecked property of the check-box, the selected index of the combo box and the value of the text box.
I was previously using a simple DataTable to populate the main grid data, but I'm going to need something more complex to hold both the grid data and the values for each column. Or perhaps I can store them as separate entities entirely.
So, does anyone have any idea of how I might pull off this binding? One limitation is that the columns must be auto-generated since I have no idea what they will be until runtime. The application simply loads the data form an Excel spreadsheet and there may be any number of columns present.
Thanks,
Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是我最终将其与 MVVM 模式结合使用的做法:
我有两组数据用于绑定到我的视图模型:一组用于实际网格数据,一组用于列标题。目前,它们公开为两个属性:
处理两组不同数据的技巧在于网格中。我对 DataGrid 进行了子类化,并为网格提供了一个名为 ColumnSource 的附加数据源,作为依赖属性。这就是绑定到我的视图模型上的 ColumnData 的内容。然后,我将每个自动生成的列的标题设置为 ColumnSource 数据源中适当索引的数据。代码如下:
我现在可以在列的模板化标题中执行正常的数据绑定,这将全部绑定到视图模型的 ColumnData 属性中的数据。
更新:我被要求显示我的网格的 XAML。这是非常基本的,但它是:
这是 ImporterColumnHeaderStyle:
Here's what I ended up doing to use this with the MVVM pattern:
I have two sets of data for binding on my view model: one for the actual grid data and one for the column headers. Currently these are exposed as two properties:
The trick to working with two differing sets of data is in the grid. I have subclassed the DataGrid and given the grid an additional data source called ColumnSource, as a dependency property. This is what is bound to the ColumnData on my view model. I then set the header of each auto-generated column to the appropriately indexed data in the ColumnSource data source. The code is as follows:
I can now perform normal data binding in the templated header of my columns, which will all bind against the data in the ColumnData property of my view model.
UPDATE: I was asked to show the XAML for my grid. It's really basic, but here it is:
And here is the ImporterColumnHeaderStyle:
我绝对是 WPF / MVVM / 数据绑定菜鸟,但最近一直在努力研究这些东西。我不知道到目前为止您已经连接了什么,但首先您需要为您的视图设置 DataContext。由于您使用的是 MVVM,我假设您有一个 ViewModel,因此它应该是您的视图的 DataContext。
即,如果您让视图创建/拥有您的 ViewModel,它可能看起来像这样:
您可以轻松地将 CheckBox、ComboBox 和 TextBox 数据绑定到 ViewModel 中的属性。我发现最简单的方法是让您的 ViewModel 从基本视图模型类继承,例如 乔什·史密斯写的。当您希望 ViewModel 通知 GUI 值的任何更改时,这将为您提供一个内部调用的方法。
假设您有 ImportColumn、LastName 和 LastNameText 等属性(所有 C# 属性,而不是相应调用 OnPropertyChanged 的字段),那么您的 XAML 将如下所示:
我希望这对您有所帮助。如果没有,请发表评论,我会尽力尝试其他事情。
I am definitely a WPF / MVVM / databinding noob, but have been working hard on this stuff lately. I don't know what you have wired up so far, but first you'll want to set the DataContext for your View. Since you're using MVVM, I assume you have a ViewModel, so that should be the DataContext for your View.
i.e. if you have your View create / own your ViewModel, it could look something like this:
You can easily databind your CheckBox, ComboBox, and TextBox to properties in your ViewModel. I have found the easiest way is to make your ViewModel inherit from a base viewmodel class, like the one that Josh Smith wrote. This will give you a method to call internally when you want the ViewModel to notify the GUI of any changes in values.
Assuming you have properties like ImportColumn, LastName, and LastNameText (all C# properties, not fields that call OnPropertyChanged accordingly), then your XAML would look something like this:
I hope this helps you out. If not, please comment and I'll try to do as best as I can to try other things out.
我们在我们的应用程序中做了类似的事情。
我所做的是派生出我自己的列类型(DataGridSearchableBooleanColumn),然后替换 DataGridColumnHeader 模板,我在其中放置了两个内容演示者。第一个我绑定到内容(与默认模板相同),第二个我绑定到列。我为该列使用了一个数据模板(其中有一些用于不同的搜索类型(文本、组合、布尔值)。然后我向该列添加额外的属性,以便我可以绑定到它们。看看这段代码是否有意义。
We do something similar in our app.
What i have done is derived my own column type (DataGridSearchableBooleanColumn), then i replace the DataGridColumnHeader template, i put two content presenters in there. the first i bind to the content (the same as the default template) the second i bind to the column. I use a data template for the column (i have a few of them for different search types (text, combo, boolean). then i add the extra properties to the column so i can bind to them. See if this code makes sense.