DataGrid WPF 中的标题模板
我需要在数据网格标题中为其中一列提供一种日期选择器。当用户从此标题日期选择器中选择日期时,系统应将此日期绑定到具有该日期的所有列单元格。
有办法做到吗?
I need to have a kind of a datepicker in my datagrid header for one of the columns. When the user selects the date from this header datepicker, the system should bind this date to all the column cells with the date.
Is there a way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的方法是将标题的 dataTemplate 设置为包含 DatePicker 的自定义模板,该模板的 Date 绑定到 DataGrid 的 DataContext 的属性之一,然后将此特定列中的单元格绑定到同一属性。
像这样的东西:
免责声明:我没有尝试过这个,并且不确定
{Binding DataContext.myDate,relativeSource={RelativeSource AncestorType=DataGrid}
的事情。您可能需要进行一些调整,但总的来说,这应该让您开始了解如何继续the best way is to set the header's dataTemplate to a custom template containing a DatePicker whose Date is bound to one of the DataGrid's DataContext's properties, then bind the cells in this specific column to the same property.
something like this:
Disclaimer: I did not try this and am not sure about the
{Binding DataContext.myDate, RelativeSource={RelativeSource AncestorType=DataGrid}
thing. You would probably have do to some adjustments, but overall, this should give you a start on how to proceed您可以修改 DataGrid 的列标题以包含 DateTimePicker,然后向 DateTimePicker 添加更改事件,该事件会在数据更改时更新该列中的所有数据。
You can modify the column's header for the DataGrid to include a DateTimePicker, then add a change event to the DateTimePicker which updates all the data in that column when the data changes.