我可以在 WPF DataGrid 中指定哪些列是可编辑的吗? [英] Can I specify which Columns are editable in a WPF DataGrid?
问题描述
我有一个带有自动生成列的 WPF 4.0 DataGrid.我只想允许用户编辑第一列.有没有简单的方法可以做到这一点?
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?
我试图添加一个 DataGridCell 样式并根据 ColumnName(第一列始终具有相同的名称)或 ColumnIndex 设置它的编辑能力,但是我无法为此找出正确的 XAML,或者即使可能.
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.
推荐答案
我明白了......这是我使用的:
I got it.... here's what I used:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<DataTrigger Value="PART_IsSelected" Binding="{Binding Path=Column.Header, RelativeSource={RelativeSource Self}}">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
如果你愿意,你可以使用 Column.DisplayIndex
而不是 Column.Header
If you want, you can use Column.DisplayIndex
instead of Column.Header
我仍然不确定为什么绑定不能直接工作并且需要由 RelativeSource 引用,但至少它可以工作:)
I'm still not sure why the binding won't work directly and needs to be referenced by a RelativeSource, but at least it works :)
这篇关于我可以在 WPF DataGrid 中指定哪些列是可编辑的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!