我可以指定哪些列在WPF DataGrid中可编辑? [英] Can I specify which Columns are editable in a WPF DataGrid?
问题描述
我正在尝试添加一个DataGridCell样式,并根据ColumnName设置它的编辑能力(第一列始终具有相同的名称)或ColumnIndex,但是我无法弄清楚正确的XAML,或者即使是可能的。
我知道了。 ...这里是我使用的:
< DataGrid.Resources>
< Style TargetType ={x:Type DataGridCell}>
< Setter Property =IsEnabledValue =False/>
< Style.Triggers>
< DataTrigger Value =PART_IsSelectedBinding ={Binding Path = Column.Header,RelativeSource = {RelativeSource Self}}>
< Setter Property =IsEnabledValue =True/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGrid.Resources>
如果需要,可以使用 Column.DisplayIndex
而不是Column.Header
我仍然不知道为什么绑定不能直接工作,需要被RelativeSource引用,但至少它可以工作:)
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.
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>
If you want, you can use Column.DisplayIndex
instead of Column.Header
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屋!