我可以指定 WPF DataGrid 中哪些列是可编辑的吗?
我有一个带有自动生成列的 WPF 4.0 DataGrid。我只想允许用户编辑第一列。有一个简单的方法可以做到这一点吗?
我试图添加 DataGridCell 样式并根据 ColumnName(第一列始终具有相同的名称)或 ColumnIndex 设置其编辑能力,但是我无法找出正确的 XAML,或者即使这是可能的。
I have a WPF 4.0 DataGrid with AutoGenerated columns. I would like to only allow the user to edit the first column. Is there an easy way of doing this?
I was trying to add a DataGridCell style and set it's editing ability based on either ColumnName (1st column always has the same name) or ColumnIndex, however I cannot figure out the correct XAML for this, or even if it is possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下面的示例可以解决一列或多列的问题
The below sample does the trick for one or more columns
每列都有一个 IsReadOnly 属性。另外,整个 DataGrid 也具有 IsReadOnly [这不会影响绑定,只会影响用户编辑字段的能力]
编辑:
急于回复,抱歉。我只能猜测,如果可能的话,您不应该自动生成列,因此您可以控制哪些列是只读的以及哪个控制模板位于何处...只需使用列的 Binding 属性,这样您就不需要自动生成它们。
Each column has a
IsReadOnly
Property. Also, the whole DataGrid has the IsReadOnly as well [This does NOT affect the binding, just the ability of the user to edit the field]EDIT:
Rushed an answer, sorry. I can only guess that you should NOT auto-generate columns if possible, so you can control which ones are readonly and which controltemplate goes where... just use the Binding property of the columns so you dont need to autogenerate them.
我明白了......这就是我使用的:
如果你愿意,你可以使用 Column.DisplayIndex 代替 Column.Header
我仍然不确定为什么绑定不能直接工作并且需要由相对源引用,但至少它有效:)
I got it.... here's what I used:
If you want, you can use
Column.DisplayIndex
instead of Column.HeaderI'm still not sure why the binding won't work directly and needs to be referenced by a RelativeSource, but at least it works :)