我可以在 WPF DataGrid 中指定哪些列是可编辑的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4471934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:24:25  来源:igfitidea点击:

Can I specify which Columns are editable in a WPF DataGrid?

wpfdatagridediting

提问by Rachel

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?

我有一个带有自动生成列的 WPF 4.0 DataGrid。我只想允许用户编辑第一列。有没有简单的方法来做到这一点?

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.

我试图添加一个 DataGridCell 样式并根据 ColumnName(第一列始终具有相同的名称)或 ColumnIndex 设置它的编辑能力,但是我无法为此找出正确的 XAML,或者即使可能。

采纳答案by Rachel

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.DisplayIndexinstead of Column.Header

如果你愿意,你可以使用Column.DisplayIndex代替 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 :)

我仍然不确定为什么绑定不能直接工作并且需要由 RelativeSource 引用,但至少它可以工作:)

回答by BMG

The below sample does the trick for one or more columns

下面的示例为一列或多列提供了技巧

  private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.Column.Header.ToString() == "COLUMNNAME")
        {
            // e.Cancel = true;   // For not to include 
            // e.Column.IsReadOnly = true; // Makes the column as read only
        }

    } 

回答by Machinarius

Each column has a IsReadOnlyProperty. 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]

每列都有一个IsReadOnly属性。此外,整个 DataGrid 也有 IsReadOnly [这不会影响绑定,只是用户编辑字段的能力]

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.

编辑:匆忙回答,抱歉。我只能猜测如果可能的话你不应该自动生成列,所以你可以控制哪些是只读的,哪些控制模板去哪里......只需使用列的 Binding 属性,这样你就不需要自动生成它们。

回答by nilam rathod

private void dgTableDetailAdj_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    foreach (DataGridColumn col in dgTableDetailAdj.Columns)
    {
        if (col.Header.Equals("columnName"))
        {
            col.IsReadOnly = true;
        }
    }
}