我可以指定哪些列在WPF DataGrid中可编辑? [英] Can I specify which Columns are editable in a WPF DataGrid?

查看:138
本文介绍了我可以指定哪些列在WPF DataGrid中可编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有AutoGenerated列的WPF 4.0 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆